Unit Testing in Theory and Practice: Test-first programming with JUnit
by JeanPhilippeBelanger and IainLowe
What is a Unit? How do I test it?
A unit is usually defined as the smallest unit that could be tested in isolation. This would lead us to expect to test one class or even one method. A unit can, in practice be a group of classes such as a the compiler unit of an application or the printing unit.
We should differtiate between unit testing and functional testing then. I think that the compiler unit may actually be a function, so the above statement is not clear
We should also emphasize the importance of limiting your tests to the unit you are testing. It's useless to test the Socket class (we expect the Java people to have run their own tests)
What is test-first programming?
Test-first programming is a different way of designing rather than a different way of coding. The design of a system is codified into UnitTests which become a sort of ExecutableSpecification?. These tests are then run in order to see what functionality is missing from the system. Functionality is only implemented when its lack causes tests to fail.
What is a testing framework?
A testing framework such as JUnit allows us to run tests in batches but individually. Each test gets its own set of data on which to operate. The framework can collect the results from all the various tests and collate the results creating reports and/or metrics.
xUnit is not necessary, but it will give you a hand. Starting with tests in a main() might be a good introduction
UnitTests - the safety net of ReFactoring
By running our suite of unit tests before and after each refactoring we insure that the function of the code has not changed. Thus we can change the design of our code without worrying about the adverse effects of those changes on other parts of the system.
Testing the tests
We don't need to test the tests. If our tests are so complicated they require their own tests then we have over-complicated the tests (see TestSmells?).
Tests should be really short (which tends to make them bug free)
Refactoring also applies to tests
What do you mean? How do you refactor tests? Hmmm. As a programmer, I see tests as a functionality. If writing tests is getting cumbersome or error prone (or repetitive), I'll refactor the test code until I get the same results for the test class I'm redoing, then I'll add new tests.
TestSmells?
In Java, tests are a great place for anonymous classes (Templates and commands)
Take a look at http://groups.yahoo.com/group/extremeprogramming/files/XPTestedDraftJan3.zip, it may give you some ideas -- jmh
Thanks! This is really cool! -- il