![]() |
Specs
Beautiful C++ Test Framework
|
Sometimes you may find yourself needing to share data between your Setup
, Teardown
, and Test
code.
Specs
provides a way to define, update, and access variables which are stored on the currently running Test
.
Important: any variables still remaining in the test variable collection after the final
Teardown
for that test will bedelete
'd.
Use current_test->var()
to set variables on the currently running Test
:
For simple values (int
, bool
, float
, etc.) you can use current_test->var(<variable name>, value)
:
For text (const char*
), use current_test->var_text(<variable name>, "text")
:
The
var_text
function manages memory for copying and storingconst char*
values.
For complex types, you should use current_test->var(<variable name>, *pointer)
with a pointer to the instance:
When accessing variables, you must specify the type of the variable you are trying to access:
To access text (const char*
), use current_test->var_text(<variable name>)
:
If you want to remove a variable from the variable collection, you can use current_test->unset(<variable name>)
:
By default, every variable set on a Test
will be delete
'd after the final Teardown
for that is run.
You can override this behavior by passing a value for the bool destructable
parameter of var()
:
The above example will not delete
the Dog
instance when the test is finished.
If you want to more explicit than passing false
, the following helpers exist:
current_test->managed_var(<variable name>, value)
current_test->unmanaged_var(<variable name>, value)
The managed_var
function is equivalent to the default behavior of var()
.
When using managed_var
, the value will be automatically deleted after the final Teardown
for the test is run.
When using unmanaged_var
, the value will not be deleted after the final Teardown
for the test is run.
You can change the "destructable" property of a variable by using current_test->set_destructable(<variable name>, <bool>)
:
If you ever need to check the "destructable" property of a variable, you can use
current_test->is_destructable(<variable name>)
.