The composite pattern
The composite pattern helps in writing proper abstractions when dealing with objects that appear in hierarchies; being containers and containees. For example, on a filesystem, a directory can contain files and other directories. In this case, the composite pattern would be a suitable abstraction. Each element of the filesystem can be either a container for other elements (a directory), or a terminal node (file).
In this section, we will see how to set up and implement the composite pattern to represent a series of unit tests organized in test suites.
Using the composite pattern to represent tests and suites
When writing unit tests, it's recommended to group together tests that are closely related to one other. We call a series of tests a suite, which can contain subsuites. Whole suites, subsuites, and individual tests can be run. These features makes the composite pattern very suitable for organizing the tests in suites, the suite itself being the composite.
Â
First, we...