A behavior-driven development testing library for C++ with an RSpec-inspired DSL.
See https://cppspec.readthedocs.org/ for full documentation and a tutorial.
C++Spec requires a compiler and standard library with support for C++20.
Note: Only the tests require being compiled with C++20 support (-std=c++20
). No other part of an existing project's build must be modified.
The recommended usage is as a subproject integrated into your build system. For CMake this would look something like below:
FetchContent_Declare(
c++spec
GIT_REPOSITORY https://github.com/toroidal-code/cppspec
GIT_TAG VERSION
)
# Or using CPM
CPMAddPackage("gh:toroidal-code/cppspec@VERSION")
Specs can then be automatically added as targets with
discover_specs(specs_folder)
This will create a separate executable for every file ending in _spec.cpp
in the given directory (recursive) and add them to CTest.
If you've ever used RSpec or Jasmine, chances are you'll be familiar with C++Spec's syntax. For example, this is a C++Spec version of the first snippet on RSpec's README.
#include "cppspec.hpp"
#include "order.hpp"
describe order_spec("Order", $ {
it("sums the prices of its line items", _ {
Order order();
order.add_entry(LineItem().set_item(Item()
.set_price(Money(1.11, Money::USD))
));
order.add_entry(LineItem().set_item(Item()
.set_price(Money(1.11, Money::USD))
.set_quantity(2)
));
expect(order.total()).to_equal(Money(5.55, Money::USD));
});
});
CPPSPEC_MAIN(order_spec);
Heavily inspired by RSpec and Jasmine.
Copyright © 2014-2024 Katherine Whitlock
The project is licensed under the MIT License.