7. Changing Style

If you already have some TDD style Cgreen test suites, it is quite easy to change them over to BDD-ish style. Here are the steps required

  • Add Describe(SUT);

  • Turn your current setup function into a BeforeEach() definition by changing its signature to match the macro, or simply call the existing setup function from the BeforeEach(). If you don’t have any setup function you still need to define an empty BeforeEach().

  • Ditto for AfterEach().

  • Add the SUT to each Ensure() by inserting it as a first parameter.

  • Change the call to add the tests to add_test_with_context() by adding the name of the SUT as the second parameter.

  • Optionally remove the calls to set_setup() and set_teardown().


If you want to continue to run the tests using a hand-coded runner, you can do that by keeping the setup and teardown functions and their corresponding set_-calls.

It’s nice that this is a simple process, because you can change over from TDD style to BDD-ish style in small steps. You can convert one source file at a time, by just following the recipe above. Everything will still work as before but your tests and code will likely improve.

And once you have changed style you can fully benefit from the automatic discovery of tests as described in Automatic Test Discovery.