CppSpec
Behaviour driven development with C++

Structure of specifications

CppSpec suite consists of specifications. Specification specifies the behavior of the context in certain state. An example of a specification is an empty stack. Specifications have behaviors, e.g. empty stack should raise an exception when popped. In CppSpec specifications are classes derived from Specification class and behaviors are member methods of these classes.

Writing specifications

The expected behavior is specified in behaviors using specify method, for example:

  • specify(context().count(), should.equal(0));
  • specify(should.be.empty());

The first example specifies that count() method of the context must return 0. The second example specifies that empty() method of the context must return true. This is a shorter form of the equivalent specification specify(context().empty(), should.equal(true)). Below are shown different kinds of specifications available in CppSpec.

Basic validation

  • specify(should.be.empty());
  • specify(not should.be.empty());
  • specify(context().count(), should.equal(1));

Exception handling

Specifying that a method must throw an exception is done using invoking syntax:

  • specify(invoking(&Stack<int>::pop).should.raise.exception<std::exception>());
  • specify(invoking(&Context::functionWithArguments, "1stArg", "2ndArg").should.raise.exception("Invalid argument"));

Invoking takes an function pointer to a context member function and possible arguments as an argument. Either class or value of the expected exception if given as an argument for exception method.

Containers

If the context implements iterator interface, you can specify that context should contain either unique items or sequences using contain keyword.

  • specify(should.have.element("key"));
  • specify(should.have.elements(sequence.begin(), sequence.end()));

Regular expressions

  • specify(context().asString(), should.contain(pattern));
  • specify(context().asString(), should.match(pattern));

Executing specifications

All specifications are compiled as executables which main function must call SpecRunner::runSpecifications. CppSpec has the following command line arguments:

  • -o --output for selecting the reporting format.
  • -s --specification for running only the specification given as argument. The parameter can be repeated multiple times if needed.
  • -h --help for help about command line arguments.

Reporting

CppSpec supports three logging formats:

  • SpecDox inspired, human readable logging to standard output
  • JUnit compliant XML reports
  • CUTE compliant logger for Eclipse integration

By default JUnit reports are generated to the working directory unless --report-dir argument is used to specify another path. The reports are named after specifications. JUnit reports can be redirected to standard output by giving --no-logs argument.

To use console reporting, -o console (or --output console) argument must be given when executing the specifications.

For Eclipse integration run specification from Eclipse as CUTE test giving -o cute as an argument.