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?

  • Expensive how? Programming time? Length of running time?
    – Euphoric
    Jun 13, 2014 at 5:20
  • @Euphoric: Excellent counter question! I state it more clearly now. Thank you.
    – phlipsy
    Jun 13, 2014 at 5:32
  • 3
    Which is problematic? Building the tree to test or creating the testing logic itself? What do you test? Is it the logic of the whole tree or do you just test whenever a component is in a tree in right spot? Did you try write any of those tests, or are you just guessing?
    – Euphoric
    Jun 13, 2014 at 5:38

1 Answer 1


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.)

  • Great suggestion: Do not construct the test data by yourself. Rather construct it based on a simple description of the tree-structure I expect. I have to think about this!
    – phlipsy
    Jun 13, 2014 at 5:56
  • It's a great strategy, especially if you can work backwards from the answer to the question.
    – david.pfx
    Jun 15, 2014 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.