The unit test :
“Test Unit (TU) (development test): A unit test is a test conducted by the developer in development environment; it is intended to demonstrate a unit complies with the criteria defined in the technical specifications.”
In the case of a procedural language unit is « the process », and in the case of an object-oriented unit is represented by « The Class. »
Unit testing is testing the first white box-type to be charged by profile type developers.
In the case of this kind of test the important question is « What is the quality of the units developed and how to measure it?
Thus, it is for the developer to test a module, independently of the rest of the program in order to ensure that it meets the requirements (functional / technical) and it works correctly. This audit is currently an audit of code coverage, which is to ensure that the test leads to perform all (or a portion) of the instructions in the code to test.
Thus, it should be understood that the unit must conduct (or the piece of code):
- Check the functions and roles of the Unity
- Test the boundaries of the unit (bounds: zero, one, complete. Search an empty collection, for example)
This results in a simple way by creating a test class per class developed.
Example of basic unit tests and represented by a class of test:
- Invoke the method with the values passed as parameters
- Observe the results
- Check if the results are those expected
Thus we must select values for which we can determine the outcome.
As we see it may take time to perform these tests manually. Similarly, if classes are complex and that we must carry out regression tests…
To help the developer tools exist and JUnit is the most effective (see details in the section on tools).
Examples of other types of unit tests:
- Audit code: verification step of lines of code against a standard development
- Bound checking tool to check any entries in the memory areas
- Documentation: reading source code automatically generating descriptions of the code
- Memory leak detectors: these tools tests memory allocation applications
- Static code (path) analyzer: it is identifying paths / code structure as measured McCabe, etc.
Unit testing tools:
- MAVEN: free software tool for the management and automation of production of Java software projects. It provided a way to synchronize independent projects: publication Standardized information vending module jar. In basic version, Maven can dynamically download material on storage software known. It offers the transparent synchronization of modules required.
- ANT : idem que MAVEN
- CVS Subversion
The testing Framework:
The best approach to undertake unit testing is to establish a « prepared environment » (or Framework) that is dedicated. In the case of development of Java type, the combination is the perfect synergy of tools below:
- Java development environment integrated with Eclipse in it:
- unit testing tools: JUnit (Tools written by the creator of the programming Xtrem Kent Beck), it allows to test the development of Java type
- Generating doc. : JavadocC# / .Net : NUnit
- C++ : CPPUnit / CUnit
- Fortran : fUnit
- Delphi : DUnit
The three basic concepts of JUnit are:
- Verdict: Each test can go GREEN (OK or pass) or red (fail or NOK)
- Test case: determining if a method produces good results for a given set of values passed as parameters:
- Represented by a method in a class test
- There have as many methods as you want in a class of tests
- There is often a test class for each class to test (one to one)
- Test suite: it contains a set of test cases for one class
The analytical coverage tests:
- Cobertura. It can produce:
- The number of classes per package
- The percentage of lines tested
- The percentage of tested expressions
- The list of components with the test unit is in error (list of anomalies by component / access services concerned)
Tools Control code:
Checkstyle: This control ensures a well-defined level of quality of source code.
The reporting tools
Weekly reporting is generated and provided through the tool MAVEN.
Reports to generate 1: Unit Testing in error
This report presents a summary of all the tests and highlights the following:
- Number of tests
- Number of errors
- Percentage of success
Reports to generate 2: Coverage of unit tests
Each anomaly must be met up through a management tool defects. Thus, the anomalies are included in the tool as tests.
The analysis should lead to:
- Changing the unit test to incorporate a change in the code. The change was made to replay developer testing
- The amendment of the Code, if a regression is found. The corrected source code is up for re-replay testing and closure of the defect if the test is validated
- The module of Quality Center Defects (ed. HP Mercury)
Reminder of good practice in unit tests:
- Use a framework absolutely
- Interface with the framework tools
- Write tests before code
- A class = test
- To test the classes in the same package (same directory) that the classes related
- Code unit tests must change with the application and must be maintained
- The name of the classes of tests must necessarily include the word TEST (to distinguish them easily and Junit 3 and found her classes)
- As for automated functional testing make sure to put the system in the same condition as before the tests (this « rehabilitation » may be part of the actions of your Selenium test scripts for example)
- How to reduce test time:
- Isolate the unit tests identified as greedy time (DB access, competition …) and run less frequently than every compilation, for example: before each filing of source code in the common code base
- Do the unit tests on the assembly that has been modified and not the entire
To write this article I helped my experiences but also the return of developer and sites such as (whom I thank for their very informative articles):
- http://fr.wikipedia.org/ (and UK version)
- http://developpez.com (http://smeric.developpez.com/java/astuces/tests/)
I advise you to report for more details on the subject.