![]() |
Specs
Beautiful C++ Test Framework
|
Specs is very easy to install if you're using xmake or vcpkg.
Specshas quite a few library dependencies which are available asxmakeandvcpkgpackages.Due to the dependencies, I do not recommend installing
Specsmanually.
Specs requires C++17 or higher.
Using C++20 works out-of-the-box without any additional dependencies.
If you are using C++17, you must add fmt as a dependency for string formatting.
Why not C++14?
Specsusesstring_viewandstructured bindingswhich are C++17 features.
The following is an xmake.lua example which sets up a Specs test suite:
That's it!
If you want to verify that it works, create a Test.cpp with the following contents:
Then build and run your test target.
You should see the following output:
By default, Specs will catch any std::exception (or literal string) thrown and display it as a test failure message.
If you want to integrate with an assertion library, we provide integrations for the snowhouse and libassert assertion libraries.
If you want to install the the Snowhouse assertion library with Specs support:
If you want to verify that it works, create a Test.cpp with the following contents:
Then build and run your test target.
You should see the following output:
If you want to install the the libassert assertion library with Specs support:
Note: at the time of writing, I was having trouble getting
libassertto build from the officialxmakeregistry.If you run into problems, feel free to use the latest version of
libassertfrom theMrowrLibregistry (using thelibassert_latestpackage):add_requires("libassert_latest", { configs = { lowercase = true } })-- # ...target("MyProject.Tests")-- # ...-- # Add the libassert and specs_libassert packages to your targetadd_packages("libassert_latest", "specs_libassert")
If you want to verify that it works, create a Test.cpp with the following contents:
Then run xmake run MyProject.Tests and you should see the following output:
Release Mode
Note:
libassert'sassertmacro does NOT throw exceptions inreleasemode.This means that
Specswill not be able to catch the assertion failure and report it as a test failure.To get around this:
- you can use the
VERIFYmacro instead ofassert(which throws exceptions inreleasemode)- (recommended) use the
assert_thatmacro provided bySpecs(which is an alias forVERIFY) ```
In this example, we use a
vcpkg.jsonfile to manage ourvcpkgdependencies.See vcpkg Manifest Mode for more information.
To install Specs using vcpkg, you will need to:
specs as a dependency in your vcpkg.jsonMrowrLib/Packages registry to your vcpkg-configuration.jsonspecs::specs in your test target in your CMakelists.txtAdd specs as a dependency in your vcpkg.json:
Add the MrowrLib/Packages registry to your vcpkg-configuration.json, including a full list of all Specs dependencies from the registry:
Note: you will want to update the
baselinevalues invcpkg-configuration.jsonfor:
- the
default-registryregistry: this should be the latest commit SHA ofmicrosoft/vcpkg- the
MrowrLib/Packagesregistry: this should be the latest commit SHA ofMrowrLib/Packages
If you want to verify that it works, create a Test.cpp with the following contents:
Then run xmake run MyProject.Tests and you should see the following output:
By default, Specs will catch any std::exception (or literal string) thrown and display it as a test failure message.
If you want to integrate with an assertion library, we provide integrations for the snowhouse and libassert assertion libraries.
If you want to install the the Snowhouse assertion library with Specs support:
specs-snowhouse to your vcpkg.jsonspecs::specs_snowhouse in your test target in your CMakelists.txtHere we add snowhouse from the official vcpkg registry, and specs-snowhouse from the MrowrLib/Packages registry:
If you want to verify that it works, create a Test.cpp with the following contents:
Then build and run your test target.
You should see the following output:
At the time of writing, I can't get the vcpkg version of libassert to build.
I couldn't get the instructions on the libassert GitHub page to work either.
For that reason, for now... you're on your own.
Here is the libassert README: https://github.com/jeremy-rifkin/libassert
Here is a recent bug report for the vcpkg: https://github.com/jeremy-rifkin/libassert/issues/75
Don't worry, I really like the elegance of
libassertand I hope to get this working!