Step 3: Develop Unit and Regression Tests
Once you begin writing code or fixing issues in existing code, there's no excuse for neglecting to properly test the code you wrote, even if the rest of the code lacks such diligently created test cases. This is the time to apply Test Driven Development (TDD) or any other technique that keeps test cases at the front ofor at least tightly linked tothe development process.
Depending on the nature of the code change, functional-level (application-level) regression tests and/or unit-level regression tests may be appropriate. Each has its own benefits. The functional-level tests are preferred in cases when tests can feasibly be set up to represent a high-level requirement to implementor a specific bug that should be addressedand the tested program can produce sufficient output to effectively validate the code's behavior (via the direct outputs it generates, log messages, debug messages, and the like). Unit tests are appropriate when test setup is complicated because the error condition itself is not a common scenario, when the changed code is buried deep in the module hierarchy, or when it's difficult to validate the test outcome at the functional level (that is, the program lacks debugging facilities). This is frequently the case with corner-case conditions or error-handling functions. These are supposed to trigger in exceptional circumstances, which would be difficult to set up in a test harness.
For example, take a real-life problem discovered in the file-processing utility in Listing Two. One of the functions of this file utility is "normalization" of the file path. If the file foo.c is said to be in directory /a/b/c/../d, the utility transforms this into /a/b/d/foo.c. However, when a directory path starts with .. (dot dot), this part of the path is discarded as a result of the code annotated with the comment // BUG; thus ../../a/b/c is transformed into /a/b/c. Listing Three eliminates this problem.
void Path::normalize(PathInfo& input) { PathElements result; PathElements::const_iterator it = input.elements.begin(); PathElements::const_iterator end = input.elements.end(); for (; it != end; ++it) { if (*it == "..") { if (result.empty()) { // BUG result.push_back(*it); } else { result.pop_back(); } } else if (*it != ".") { result.push_back(*it); } } input.elements.swap(result); }
void Path::normalize(PathInfo& input) PathElements result; PathElements::const_iterator it = input.elements.begin(); PathElements::const_iterator end = input.elements.end(); for (; it != end; ++it) { if (*it == "..") { if (result.empty() || result.back() == "..") { // BUG fixed result.push_back(*it); } else { result.pop_back(); } } else if (*it != ".") { result.push_back(*it); } } input.elements.swap(result); }
Additionally, tests have to be created to verify the fix. When considering tests to validate the fix, the choice of tests is influenced by the fact that this function is part of a common library, and both the defect and fix are completely contained within the function. Rather than create a functional test for the file utility, it's actually easier to create a unit test for the fix using an appropriately crafted string for a file path:
void PathTest::testGetNormalized() { Path test ("../../a/../b/c/./d/../e"); Path result = test.getNormalized(); CPPTEST_ASSERT_CSTR_EQUAL ("../../b/c/e", prepareNormalizedPathString (result.toString()).c_str()); }
This unit test can be complemented by the functional test of the file utility, as long as it can reliably capture the utility's output for the specific file and verify that it is correct.
No matter what type of tests you create for a new code fragment, they must be added to the established regression suite and be verified on a regular basis. This assures that if subsequent changes break this functionality, the problem will not go unnoticed.