![]() |
Specs
Beautiful C++ Test Framework
|
Sometimes you may find yourself writing the same code in your Setup
and/or Teardown
for many test groups.
In these scenarios, it may be useful to create a shared code template.
A template is a sharable group of:
Setup
codeTeardown
codeOneTimeSetup
codeOneTimeTeardown
codeTest
codeTestGroup
, StartTestGroup
, or Describe
)There are a few available syntax options for defining templates:
#define SPEC_TEMPLATE Template_Name
TestTemplate("Template Name")
/EndTestTemplate()
StartTestTemplate("Template Name")
/EndTestTemplate()
This is a great option for defining a templates in their own isolated files (and it's my personal favorite!).
Note: when using
#define SPEC_TEMPLATE
, the name of the spec template has underscores removed.
TestTemplate
allows you to define a template anywhere in your code.
Don't forget to call
EndTestTemplate()
!
StartTestTemplate
allows you to define a template anywhere in your code.
The difference is that it supports a nested { ... }
syntax.
Don't forget to call
EndTestTemplate()
!
If you define your templates in .cpp
files, you'll need to make sure they are loaded before your tests.
Otherwise, your tests won't be able to find them!
In your xmake.lua
or CMakeLists.txt
file, make sure your template files are loaded before your test files.
For example, you might want to separate your templates into a Templates
directory.
Note: you can safely define your templates in
.h
files as well. They will only be defined once.
This is the easiest part of all!
Simply use the UseTemplate("Template Name")
function to use a template.
If you are using lambda syntax inside of a Describe
block, use the use_template("Template Name")
function instead.