Unit tests (=component tests) check whether the components written by the developers work as they intend. In agile methods, the aim is to run the unit tests very frequently for quality assurance purposes. This can only be achieved if the tests are fully automated, i.e. they are themselves a program whose execution requires no more effort than pressing a button.
It is common (but not mandatory) that the test is written in the same language as the test object. In agile software development, massive use is made of unit tests as part of test-driven development and refactoring.
What is the purpose of unit tests?
The role that unit tests play in agile software development differs significantly from the role they play in classical approaches. For example, at the time of their creation in test-driven development, they are used to guide the design of the software rather than to find defects. In the context of the incremental design they are indispensable, in order to keep also in the long term changeable code. Thus, they are an important prerequisite for refactorings. Thanks to the tests, accidental changes in behavior are uncovered at the push of a button. Unit tests also help when new features are added to an existing system: They reveal unintended remote effects of the new functions on existing system parts. Furthermore, they also help to document the system by clearly showing intended uses and reactions of the test object.
What is JUnit?
Kent Beck, the father of XP, proposed (at that time for Smalltalk) a small framework that supports the creation of such automated tests. Meanwhile, this SUnit framework can be considered the father of a whole family of testing frameworks for different programming languages. The representative of this family for Java is the JUnit framework, in the development of which both Kent Beck and Erich Gamma were involved.
What does a unit test look like?
The following example shows a simple test. It should be checked that the comparison operation for a Euro class is implemented correctly.
Who writes the unit tests?
In heavyweight methodologies, there is often a separate team that is responsible for quality assurance and tests the code of the development team. In agile approaches, there is no such separation: Especially in test-driven development, unit tests are always written by the same developers who develop the component under test.
What makes a good unit test?
… are isolated: they are independent of each other, so that the order in which they are executed does not affect the test result. If one test fails, it does not cause other tests to fail.
… each test ensures exactly one property. A problem always manifests itself in exactly one failed test.
… are completely automated, so that they are still executed frequently even under increased project pressure.
… are easy to understand and short.
… have a code quality as high as the production code itself (redundancies, code conventions, …).
… test relevant code (and e.g. no getters/setters).
… are grouped around a test fixture, not around a class.
… are written before the code under test (see test-driven development).
Of course, there are numerous other criteria for good tests. These can be found in the literature listed at the bottom of this page.