Unit tests are typically written and run by software developers
to ensure that code meets its design and behaves as intended. It is important to write the unit test code early and update it as the application code changes. Unit testing is very important in all phases of the development cycle.
Unit Testing: Benefits
is written. When the tests pass, that code is considered complete.
make sure that it works properly.
Unit Testing: Benefits
- Finds Problems easily
is written. When the tests pass, that code is considered complete.
- Facilitates change
make sure that it works properly.
- Simplifies integration
By testing the parts of a program first and then testing the sum of its
parts, integration testing becomes much easier. But an elaborate
hierarchy of unit tests does not equal integration
testing.
- Documentation
Unit testing provides a documentation to the system. Unit test cases
contain certain characteristics that are critical to the success of the
unit.
These characteristics can indicate use of a
unit.
- Design
Each unit test can be seen as a
design element specifying classes,
methods, and observable behaviour. The following Java example will help
illustrate this point.
class SanityCheck(unittest.TestCase): def testSanity(self): """fromRoman(toRoman(n))==n for all n""" for integer in range(1, 4000): numeral = roman.toRoman(integer) result = roman.fromRoman(numeral) self.assertEqual(integer, result)
The above given code is a test class that checks the correctness of a
program that converts the integer given to roman numeral and vice
versa. Here the test class checks whether the given integer falls in the
range (0,3999).(The conversion program works only for integers
inside that range). The complete conversion and the corresponding unit
test program is available here.
The above given code segment is just an example to illustrate the form of
a test class. There are certain points to be noted here.
* To write a test case, first subclass the TestCase class of the unittest
module. This class provides many useful methods which you can use in
your test case to test specific conditions.
* The TestCase class of the unittest provides the methods like
assertEqual(),assertRaises() etc. assertEqual() compares its arguments
and if not same, will raise an exception and the test will immediately be considered
failed.
Unit testing: Limitations
- Testing cannot be expected to catch every error in the program. It is impossible to evaluate every execution path in most trivial programs.
- Requires a number of lines of codes in unit test code to test a single line of code.
- Challenge of setting relevant initial condition so that the part of the code being tested behaves as the part of the whole system.
- Must keep rigorous discipline and must maintain a version control system.
- It is also essential to implement a sustainable process for ensuring that test case failures are reviewed daily and addressed immediately.
The above points explains some basics of unit testing. Please refer more details.
Unit testing is well explained with the help of above said conversion example here.
Comments
Post a Comment