Verifying graph implementations using a shared test suite

As we mentioned in the previous sections, we will be building both an in-memory and a database-backed implementation of the Graph interface. To this end, we need to come up with a set of comprehensive tests to ensure that both implementations behave in exactly the same manner.

One way to achieve this is to write the tests for the first implementation and then duplicate them for each additional implementation that we may introduce in the future. However, this approach doesn't really scale well: what if we modify the Graph interface in the future? We would need to track down and update a whole bunch of tests that might be scattered across different packages.

A much better, and cleaner, approach would be to come up with a shared, implementation-agnostic test suite and then just wire it to each underlying graph implementation. I opted for this approach as it reduces the amount of maintenance that's required, while at the same time allowing us to run exactly the same set of tests against all implementations: a fairly efficient way of detecting regressions when we change one of our implementations.

But if the test suite is shared, where should it live so that we can include it in all implementation-specific test suites? The answer is to encapsulate the suite into its own dedicated testing package that our regular test code can import and use where it's needed.

The SuiteBase definition lives in the Chapter06/linkgraph/graph/graphtest package and depends on the gocheck [11] framework, which we introduced in Chapter 4, The Art of Testing. The suite includes the following groups of tests:

To create a test suite for a new graph implementation, all we have to do is to define a new test suite that does the following: