Structuring Unit Tests
Problem
You want to structure tests to simplify testing.
Solution
Notice how Recipe 13.4 embedded test data in the
test stylesheets. Each test element contains a test
num attribute and the correct result in the form
of an ans attribute. The test driver then extracts
these test elements from the stylesheet, executes the test, and
compares the expected answer against the actual answer. The most
important aspect of the test driver is that it produces no output
when the test succeeds.
Discussion
Some of the best advice on automating testing is in Brian W. Kernighan’s and Rob Pike’s The Practice of Programming (Addison Wesley, 1999). The authors state that test programs should produce output only when tests fail. Why? Who wants to wade through pages of test output to look for cases where the test fail? If you expect test code to produce no output, you will quickly notice failures when there is output. Of course, you should test your test code to make sure it actually executes before relying on this testing technique.
The method that stores the answer as an attribute in the test element
works for simple tests that produce a primitive result. However, some
templates produce node sets. In this case, you might need to store
the correct answer as child elements in the tests. You can then use
the value set operations of Recipe 7.2 to compare
results. However, sometimes you can test node-set producing templates
more simply. Consider the test driver for the
math:lowest ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access