Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,204 members, 7,825,770 topics. Date: Sunday, 12 May 2024 at 10:48 PM

Unit Testing - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Unit Testing (3402 Views)

Let's Develop A Superb Online Polling Unit / Testing Programmers: Puzzles Or Web Applications? / The Importance Of Software Testing And Not Just Software Programming (2) (3) (4)

(1) (2) (Reply) (Go Down)

Unit Testing by harryobas: 2:00am On May 22, 2012
Are there any developers on nairaland that do automated unit testing using any of the xunit flavors. As a developer myself i believe that writing unit tests is as important as writing production code. Test driven development (TDD) which is an important XP discipline (extreme programming) advocates writing test before any line of production code is written. I must admit TDD is a difficult discipline to follow and master and writing unit tests can be daunting and time consuming at first but that should not deter you from unit testing your code. Testing gives confidence to you as a developer that your software behaves as it should.
Re: Unit Testing by ektbear: 6:24am On May 22, 2012
I use RSpec for testing rails stuff. This isn't xunit-related though, to my knowledge.

I agree with you. Testing is critical. Writing tests helps you crystallize in your mind what your application should do. And it helps you quickly catch bugs in changes that you make to your code base that break your application.

I sort of don't even know how I wrote code before w/o writing tests. Seems so risky and error-prone for any non-trivial app, in hindsight.
Re: Unit Testing by harryobas: 11:15am On May 22, 2012
I am test infected and i just cannot see myself working without tests anymore. It has become a norm for me as a developer.
Re: Unit Testing by Kobojunkie: 2:01pm On May 22, 2012
^^ so are you able to volunteer some of those test development skills?
Re: Unit Testing by harryobas: 2:11pm On May 22, 2012
@kobojunkie Be specific are u currently working on a project that requires that u write unit tests or u just want to learn how to write unit tests.
Re: Unit Testing by Kobojunkie: 6:29pm On May 22, 2012
harryobas: @kobojunkie Be specific are u currently working on a project that requires that u write unit tests or u just want to learn how to write unit tests.

Ofcourse I intend to do it TDD but it would be great if we had someone write tests for us, even before we start. I am referring ofcourse to the 2012 Summer of code thread.
Re: Unit Testing by ektbear: 8:20pm On May 22, 2012
Honestly, probably you guys should write tests before you even start the project. Tests are also a specification of how your code should behave.

So whoever came up with the idea for the project, or those who have a sense of what the end result should look like should be the ones writing tests.
Re: Unit Testing by harryobas: 8:40pm On May 22, 2012
@ekt bear Unit testing is developer-centric in nature which means it is the responsibility of the developers to write unit tests. Except of course you are talking about functional and acceptance tests.
Re: Unit Testing by ektbear: 8:47pm On May 22, 2012
Hmm. I'm actually not 100% sure about the terminology differences. But my point is, assuming that you have a rough idea of how the different objects in your model should interact, then you should be able to start writing tests.

If there is a Bank class, with a method deposit(User, amount) and a User class with a field cash_in_pocket, then I should be able to write tests for that, whether i was the main developer or not.

The test specifies how the objects should interact. Anyone (developer or user of the code) should be able to write tests.
Re: Unit Testing by harryobas: 9:30pm On May 22, 2012
@ekt bear I honestly do not see how you can separate unit testing of a module about to be developed from the developer of the module. In fact TDD encourages this unit testing and developer tight coupling by advocating the test > code > refactor iterative development process.
Re: Unit Testing by ektbear: 9:44pm On May 22, 2012
I don't know it works in other settings. But for a recent web app project I hired I contractor for, I wrote the tests. Then he wrote the code to pass those tests.

For the next stage of the app, if I hire him for it, I'll also write the tests again. Then he'll write code that passes the tests.
Re: Unit Testing by harryobas: 10:50pm On May 22, 2012
@ekt bear TDD does require that you write all your test up front before writing code it should be done iteratively along with coding and refactoring the disadvantage of writing tests up front is that requirements can change mid way through the project.
Re: Unit Testing by ektbear: 11:41pm On May 22, 2012
Of course, it is an iterative process.

You don't simply write the tests blindly and expect that they won't change at all.

You write some tests, write some code, perhaps revise your tests, write some more code, etc.
Re: Unit Testing by harryobas: 1:45am On May 23, 2012
The only reasonable and sensible way to share the work of writing unit tests and writing production code between two different developers i think is through peer programming
Re: Unit Testing by Seun(m): 3:33am On May 23, 2012
I've never been able to value Unit Testing, even though a lot of people swear by it. I believe in reducing the number of lines of code in a project but Unit Testing creates more code that must be debugged and updated. I also feel Unit Testing probably doesn't catch bugs that span across units. I find that writing code carefully eliminates most errors, and for a web application, you can catch the remaining bugs by simply watching the error log. Writing code carefully involves using straightforward algorithms and writing them elegantly. It means understanding exactly what your code is doing instead of randomly changing things until it seems to work. However, a lot of smart people swear by Unit Testing so there's must be some value to it; perhaps it's more important in large teams.
Re: Unit Testing by ektbear: 4:10am On May 23, 2012
Seun, I think you should give it a shot.

Here is the deal. So I've been working part time on this little web app over the past few years. Just a little fun personal project.

And sometimes I have to take 3 or 6 months at a time in which I make no progress on the code, don't get to touch it at all.

And me, I forget a lot of stuff. So how can I confidently modify the code 3 or 6 months later? How do I know that a change I make won't break things and introduce a lot of bugs?

Am I going to waste 1 hour every time I make a change, clicking through every part of the application and manually verifying that my change didn't introduce a bug? My application is way too complicated for that.

Rather than wasting my time manually checking that I didn't break something, I can instead just run my tests again, verify that nothing broke.

Then when I add a new feature, write a test for it.

I would have been totally screwed without tests.
Re: Unit Testing by ektbear: 4:13am On May 23, 2012
The guy I hired to help me. There were several candidates available. One of them knew absolutely nothing about writing tests.

It scared me, and I ended up not hiring him. Why?

Because if he can't write tests for his code, then how will I know that it works as it should? How do I know it won't break later?

Or that there are some subtle differences between the code he wrote for his system that cause them not to work for mine?

Testing helps you catch all of that.

I would never hire a programmer who doesn't understand the value of tests for his code.
Re: Unit Testing by ektbear: 4:15am On May 23, 2012
Finally, in your case, there are excellent testing tools for Python. One in particular (Doctest) is really fast and easy to use. At a minimum, you should be doing that for your functions.
Re: Unit Testing by Kobojunkie: 4:27am On May 23, 2012
harryobas: @ekt bear Unit testing is developer-centric in nature which means it is the responsibility of the developers to write unit tests. Except of course you are talking about functional and acceptance tests.

Yes, TDD itself is developer-centric, but not all tests are written by the code writer. You could help with some of those, if nothing else. We will definitely need quite a bit of QA work done. Again, referring to summer of code 2012. grin
Re: Unit Testing by logica(m): 6:00am On May 23, 2012
Seun: Unit Testing creates more code that must be debugged and updated.
Dude, you debug Unit Tests? What is there to debug in a set of methods consisting of 1 to less than 10 lines of code that merely call target (test) methods and do assertions etc?

Seun: I also feel Unit Testing probably doesn't catch bugs that span across units.
Dude, ever heard of Integration Testing (which could actually be a suite of Unit Tests)?

Seun: I find that writing code carefully eliminates most errors, and for a web application, you can catch the remaining bugs by simply watching the error log.
LOL. So you are going to sit down and watch the error log. The error log does not even give you confidence that your application is bug free; as by some design, a situation that throws up an exception may never occur as you sit there watching (until of course when it goes live: the worst possible moment - check Finagle's Law).

Seun: Writing code carefully involves using straightforward algorithms and writing them elegantly.
I suspect you have never really been involved in the development of complex applications.

Seun: It means understanding exactly what your code is doing instead of randomly changing things until it seems to work.
Is that what you think the purpose of a Unit Test is - to randomly change code till it works? Read up on a Black-box Test.

Seun: However, a lot of smart people swear by Unit Testing so there's must be some value to it.
And why do you think they do?
Re: Unit Testing by melvournis(f): 7:08am On May 23, 2012
I don't know it works in other settings.[img]http://www.50centloseweight.com[/img]
Re: Unit Testing by lordZOUGA(m): 7:32am On May 23, 2012
seriously guys.. Tests? Tests is something you must write for every program. every programmer must have written tests in someway or the other, knowingly or unknowingly. That it is given some fancy name doesn't make it any special than it is. It's jus a way to verify that your code won't crash given a set of data. Tell me which programmer that doesn't do that?
Re: Unit Testing by moderattor: 7:55am On May 23, 2012
my blood cells use PHP unit!
Re: Unit Testing by farouqzaib: 7:59am On May 23, 2012
Learnt a lot from this thread. I use unittest for my Python code. And if u have to test code which are dependent, u can always mock the object. Seeing as this is about tests, anybody ever used Selenuim to automate browser tests? IMHO, I think it is more work than is actually needed.
Re: Unit Testing by Stevenjel: 8:00am On May 23, 2012
Get the latest jobs in Nigeria on . They have more jobs than anyone else. And it’s free to search and apply for jobs.http:///J1ZVjI
Re: Unit Testing by ektbear: 8:06am On May 23, 2012
farouqzaib: Learnt a lot from this thread. I use unittest for my Python code. And if u have to test code which are dependent, u can always mock the object. Seeing as this is about tests, anybody ever used Selenuim to automate browser tests? IMHO, I think it is more work than is actually needed.

I wrote my tests using something called Webrat (https://github.com/brynary/webrat) that simulates a browser.

Those tests tend be to pretty slow, though.

But I like how they map directly to what a user of your application will experience.
Re: Unit Testing by farouqzaib: 8:44am On May 23, 2012
Same with Selenium. You simulate a visitors experience. Too much work!
Re: Unit Testing by kpolli(m): 9:20am On May 23, 2012
Am not a developer but am a tester grin

But mehn still on manual testing level
Re: Unit Testing by AZeD1(m): 9:25am On May 23, 2012
Never done any unit testing.. Hope to peep and see if its a better way asap.
Re: Unit Testing by harryobas: 11:10am On May 23, 2012
Unit testing can also be called developer test because it is test performed by developers for developers. As a developer if i want to find out how an application should work i usually find it more useful to read the various unit tests of the application than the requirements document unless of course the application does not have unit tests.
Re: Unit Testing by candylips(m): 1:06pm On May 23, 2012
harryobas: Test driven development (TDD) which is an important XP discipline (extreme programming) advocates writing test before any line of production code is written. I must admit TDD is a difficult discipline to follow and master and writing unit tests can be daunting and time consuming at first but that should not deter you from unit testing your code.

TDD is a waste of time and terribly difficult to do right in d purist form.

Why should i write my test first or test->develop->refactor

How about testing last or testing whenever i feel like.
sometimes due to time pressures i just want to get something out there prove that it is valuable to the users and then afterwards invest time in writing tests. Why should i waste my time writing fancy tests for a feature that users might not like ?

I personally value testing based on users feedback more than testing based on regression feedback
Re: Unit Testing by candylips(m): 1:18pm On May 23, 2012
Seun: I've never been able to value Unit Testing, even though a lot of people swear by it. I believe in reducing the number of lines of code in a project but Unit Testing creates more code that must be debugged and updated. I also feel Unit Testing probably doesn't catch bugs that span across units. I find that writing code carefully eliminates most errors, and for a web application, you can catch the remaining bugs by simply watching the error log. Writing code carefully involves using straightforward algorithms and writing them elegantly. It means understanding exactly what your code is doing instead of randomly changing things until it seems to work. However, a lot of smart people swear by Unit Testing so there's must be some value to it; perhaps it's more important in large teams.

I understand your angle seun. However i believe every app requires some level of unit testing.

The questions is if these tests should be written first or when u encounter bugs like u do when u check your error logs . . Or when u have the spare time to write them !

(1) (2) (Reply)

Should It Be Java Or Python For Game Development? / Can You Solve PHP Problems? Let's Find Out.. / Python Django Android...

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 53
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.