RSpec is not merely a BDD test framework, it also packages a set of classes and methods you can use to build test utilities on top of RSpec stack. We will take a look at Rspec.configuration, RSpec::Core::Reporter, RSpec::Core::Formatters and RSpec::Core::Runner.

Rspec.configuration

RSpec reads command-line configuration from global ~/.rspec file or project specific .rspec file in the project folder. What runs underneath is Rspec.configuraion that allows setup those command-line options programmatically. For example, before test execution, you can set command-line output pass or fail colour by:

RSpec::Core::Formatters, RSpec::Core::Reporter

RSpec ships a list of formatters, default is progress formatter. There are several ways to configure a formatter through configuration, or even custom formatters. The following snippet adds multiple formatters to the configuration:

The drawback is that you have to loop through the formatters array to obtain a target formatter to make further test result manipulation. The following example gives more control over formatter objects, it also shows how to construct an RSpect reporter instance:

Creates JSON Report with Reporter

Reporter can be passed to the configuration by setting the @reporter instance in RSpec.configuration. The following is the full code example contains:

  • an example spec test, including one pass, fail and pending
  • rspec test runner
  • hashed result produced by JSON formatter and reporter

References

Leave a comment