views:

1732

answers:

18

So I've put this off for a good long time and I think it's time to stop procrastinating. I need to learn how to do unit testing, but I don't know where to start! Does anyone have any good, preferably online, resources on the subject?


Exact Duplicate: What is your single favorite book or online resource for getting started with unit testing?
Exact Duplicate: Can people recommend some good books and resources on unit testing?
.. and others

+6  A: 

http://www.masukomi.org/talks/unit_testing_talk_2/index.xul?data=slide_data.txt#page1

Best presentation on unit testing I ever found.

EDIT: Only works in browsers using the Gecko-Engine (Firefox, SeaMonkey, ...) and is 11 MB large.

VVS
Is it just me or your link only works in geko browsers? I cannot launch it in Safari.
Sergio Acosta
You're right.. too bad.
VVS
but it's worth starting up Firefox to get it to run, it's awesome.
Epaga
yea short, funny and informative.
melaos
Fantastic resource!
Kyle W. Cartmell
+2  A: 

http://xunitpatterns.com/

Lou Franco
Absolutely my favorite book about unit testing. It helped me "get" unit testing by showing me what good unit test code looks like, and how to get there from the mess I was writing before I read the book.
christopher_f
+5  A: 

Refactoring by Martin Fowler is a good place to start.

Refactoring: Improving the Design of Existing Code

Along with reading the documentation for nUnit.

www.nunit.org

Refactoring is really helpful because it places unit testing in context. Namely it use in conjunction with Refactoring. Refactoring is making a series of small testable changes to your code to change its design. Something that is done a lot when maintaining an existing piece of software.

RS Conley
nUnit is also good because has been ported to work with many different langauges. So you can see the same concepts expressed in a number of ways.
RS Conley
just for the record, nunit itself is a port of junit.
Peter Recore
+5  A: 

I like the Google Testing Blog. They have information on testing python and java, but the principles apply to unit testing in general. Some topics are more advanced, but many are useful for beginners.

Jim
And an example in LOLCode apparently. :-p
docgnome
+1  A: 

I read a book called Test Driven Development which walks you through a project developed in a test driven manner.

It's really about trying to attain a new mindset: Only write a line of production code, when it is written to get a test working.

Nathan Feger
There are a few books with titles similar to the one you quoted. Could you please update your response with a publisher or Amazon link?
Nate
+1  A: 

Just do it, pick a framework in the language that you work with, or don't even pick a framework, write your own little test objects. Try and test every functionality that you produce, try testing first, unit testing is more of a mind set and a practice than sticking to somebodies rules. Any test you write is better than none.

Any framework that you pick will have an introduction to unit testing the Wikipedia entry Test Driven Development is a good start as any.

Harald Scheirich
A: 

As wel as reading books and the like, I strongly suggest picking an open-source product known to have very good unit tests for itself, and use them as a good-practise guide. For example, Spring's test coverage is second-to-none, and they're excellent examples of how to (a) write tests, and (b) write components to be more conducive to being tested effectively.

skaffman
A: 

Craig Shoemaker did a couple of episodes of the Polymorphic Podcast on Unit Testing. He did it based on Visual Studio 2003, but I was still able to follow along with Visual Studio 2005.

Slapout
+3  A: 

I actually just wrote about this the other day and I tried to provide some very simple examples to help people get their feet wet.

http://www.peterfranza.com/2008/09/23/unit-testing-a-short-primer/

http://www.peterfranza.com/2008/09/24/unit-testing-user-interfaces/

pfranza
A: 

Agree with the person who mentioned Test Driven Development by Kent Beck - this is the book that probably influenced me most as it goes down to the lowest level concepts of why we want to do it in the first place. I like his writing style too - definitely one to read all the way through.

Jennifer
A: 

I second what @Harald Scheirich said. Reading books and articles is great and there are some good ones listed in this thread.

What you really need to do is start writing tests.

I'd recommend starting on a new project (see this question about why it would be hard to introduce into a mature code base).

There is another relevant question about advice for starting TDD.

Hamish Smith
+3  A: 

I would recommend learning not only unit testing, but also Test-Driven Development (TDD). TDD is a programming technique where the code is written, along with it's design and it's unit tests, at the same time. This leads to a very good unit test coverage, which permits easy and safe refactoring of your code. Therefore the code produced is flexible, and can be adapted to unforeseen requirements.

To get started, I can recommend the bowling game episode which shows two programmers implementing a bowling score computation, step by step, with unit tests and ending with a different design than they initially envisioned. This articles is way shorter than a book but is long enough to demonstrate the technique.

Then, to go further, there are Kent Beck's excellent Test-Driven Development: by example and Dave Astels Test-Driven Development: a practical guide (didn't had the chance to read that one yet. Fowler' Refactoring, already recommended here leads to the next level.

philippe
+2  A: 

I have started to learn unit testing couple of times. I think that it is one of the harder things to learn in programming without a good tutor. There are so many pitfalls ... It is easy to test a simple class that's what most of the examples in documentations are about. The problem comes with learning how to isolate your classes, not doing integration tests without realizing it and so on. I haven't read the books mentioned above (only looked into some), but the one that I have read and would reccoment is this The Art of Unit Testing. It describes the pitfalls and actually explains a lot about the practice of unit testing.

http://artofunittesting.com/

I would argue that since unit testing is a major concept it is extremely usefull to understand it. Reading one article out of context really didn't cut it for me. I thought I understood what I was doing, but later found my tests were rubbish ...

I strongly reccomend you to try understand the concept and try it on some really small project before jumping onto creating production code tests. Otherwise you'll throw most of them away ...

Tomas Pajonk
A: 

There are other (and many similar) suggestions in a related question.

Ola Eldøy
A: 

I would recommend looking at screencasts, to get a feel for how TDD is applied. At Dnr TV there are two episodes with JP Boodhoo, where he gives an introduction to test driven development:

If you want to see unit testing and TDD used together with a whole bunch of other agile practices, I would recommend watching the sceencast series Autumn of Agile. This series shows the development of a fully unit tested application from start to finish.

Erik Öjebo
A: 

Here's a good one-page summary of unit testing from Pragmatic Programmers. It's an appendix from their book Pragmatic Unit Testing. The book comes in a C# edition and a Java edition. That book is short and easy to read. I'd recommend you start with something like that before reading something more advanced like Fowler's refactoring book.

John D. Cook
A: 

Pragmatic Unit Testing from the PragProg Starter Kit is a great introduction.

orip
+2  A: 

The funny thing is that I just recently did what you are asking for, learned how unit test work...

What my conclusion was, is that you basically have 2 programs, the normal program and the test program. The test program calls the normal functions, and tries to decide if the response are correct! And then you save the test program so that you can verify your test from time to time.

It turned out that I have been doing nearly this for quite some time, but not like that and definitely not with any framework...

Before when I wrote a new function, I always called it a couple of times with different inputs and watch what happened. Most of the time something was wrong so I would fix that and then run the test input again.... and so on, and so on until I was happy with this function. The problem was that when I was "done" I would clean up the function that was calling the new function, and move on!!!

This is where unit test is different, here you are supposed to save all those test calls so you can repeat them later if you need. And since they are in another program, your main program is clean from any "test code".

Then you pick up a framework to help you do this, but as philippe told me in this question, even 4 lines of c code can be called a unit test framework, just have a look at minunit.

Hope this could help you in some way.

Johan