I have a rather large collection of classes which check and mutate a given data structure. They can be composed via the composition pattern into arbitrarily complex tree-like structures. The final product contains a lot of these composed structures.
My question is now: How can I test those? Albeit it is easy to test every single unit of these compositions, it is rather expensive to test the whole compositions in the following sense:
- Testing the correct layout of the composition-tree results in a huge number of test cases
- Changes in the compositions result in a very laborious review of every single test case
What is the general guideline here?
3
If something is tedious, it often can be automated – that is true for test data generation as well as for many other tasks.
Instead of constructing each test case manually, write a test case generator, which takes some simple textual description of the tree (maybe some kind of “mini DSL”) and constructs it. Write a second function which takes the composition tree and reduces it to the textual desciption again. Now it will be easy to create a data-driven test: create a bunch of tree descriptions and let the test verify that the conversion to the composition tree and the following reconversion to the text will always reproduce the original input.
(Remark: here is a former post on PSE where the idea of using a DSL for tree-like structures was explained in a different context.)
2