Contexts, setups and teardowns
Using Contexts
Each test has context attribute for storing and passing state
to sub-tests. Each test has a unique object instance of Context class however,
context variables from the parent
can be accessed as long as the same context variable is not redefined by the current
test.
The main use case for using context is to avoid passing along common arguments
to sub-tests, because contexts enable them to pass automatically.
Also, test clean up functions can be added to the current test using context.
See Cleanup Functions.
Here is an example of using context to store and pass state.
1 | from testflows.core import * |
and you can confirm this by running the test program above.
1 | Sep 24,2021 10:24:54 ⟥ Module regression |
✋ You should not modify parent’s context directly (
self.parent.context). Always set variables using the context of the current test, either by usingself.contextorcurrent().context.
Using in Operator
You can use in operator to check if a variable is set in context.
1 | # check if variable 'my_var' is set in context |
Using hasattr()
Alternatively, you can also use built-in [hasattr()] function.
1 | note(hasattr(self.context, "my_var")) |
Using getattr()
If you are not sure if context variable is set, you can use built-in getattr() function.
1 | note(getattr(self.context, "my_var2", "was not set")) |
Using getsattr()
If you want to set context variable to a specific value, if the variable
is not defined in the context, use getsattr() function.
1 | from testflows.core import Test, note |
Arbitrary Variable Names
If you would like to add a variable that, for example, has empty spaces
and therefore would not be valid to be referenced directly
as an attribute of the context then you can use setattr() and getattr()
to set and get the variable respectively.
1 | with Test("my test") as test: |
Setups and Teardowns
Test setup and teardown could be explicitly specified using Given and Finally steps.
For example, Scenario that needs to do some setup and perform clean up as part of teardown can be defined as follows using explicit Given and Finally steps.
1 |
|
✋ It is recommended to use a decorated Given step that contains
yieldstatement in most cases. See Given Withyield.
✋ Given and Finally steps have MANDATORY flag set by default, and therefore these steps can’t be skipped.
✋ Finally steps must be located within
finallyblocks to ensure their execution.
Common Setup and Teardown
If multiple tests require the same setup and teardown and the result of the setup can be shared between these tests, then the common setup and teardown should be defined at the parent test level. Therefore, for multiple Scenarios that share the same setup and teardown, it should be defined at the Feature level and for multiple Features that share the same setup and teardown, it should be defined at the Module level.
For example,
1 | from testflows.core import * |
Handling Resources
When setup creates a resource that needs to be cleaned up, one must ensure that Finally step checks if Given has actually succeeded in creating the resource that needs to be cleaned up.
For example,
1 |
|
Multiple Setups and Teardowns
When a test needs to perform multiple setups and teardowns, then multiple Given and Finally can be used.
✋ Use And step to make test procedure more fluid.
1 |
|
✋ TE flag is always implicitly set for Finally steps to ensure that failure of one step does not prevent execution of other Finally steps.
Therefore,
1
2
3
4
5 with Finally("first clean up"):
do_first_cleanup()
with And("second clean up"):
do_first_cleanup()is equivalent to the following.
1
2
3
4
5 with Finally("first clean up", flags=TE):
do_first_cleanup()
with And("second clean up", flags=TE):
do_first_cleanup()
Given With yield
Because any Given step usually has a corresponding Finally step
TestFlows supports yield statement inside a decorated Given step
to convert the decorated function into a generator that
will be first run to execute the setup and then executed
a second time to perform the clean during the test’s teardown.
✋ It is an error to define a Given step that contains multiple
yieldstatements.
1 | from testflows.core import * |
Executing the example above shows that the Finally step gets executed at the end of the test.
1 | Sep 07,2021 19:26:23 ⟥ Scenario my scenario |
Yielding Resources
If Given step creates a resource, it can by yielded
as a value.
For example,
1 | from testflows.core import * |
produces the following output.
1 | Sep 07,2021 19:36:52 ⟥ Scenario my scenario |
Cleanup Functions
Explicit cleanup functions can be added by calling Context.cleanup() function.
For example,
1 | from testflows.core import * |
produces the following output.
1 | Sep 07,2021 19:58:11 ⟥ Scenario my scenario |
Returning Values
A test is not just a function but an entity that can either be run within caller’s thread, in another thread, in a different process, or even on a remote host. Therefore, depending on how a test is called, returning values from a test might not be as simple as when one calls a regular function.
Using value()
A generic way for a test to return a value is by using value() function. Test can call value() function to set one or more values.
For example,
1 |
|
The values can be retrieved using values attribute of the result of the test
1 | with Test("my test"): |
and using the value attribute of the result you can get the last value.
1 | with Test("my test"): |
Note that if the decorated test is called as a function
within the same test type, the return value is None
if the test function did not return any value using the return statement.
1 | with Step("my step"): |
But if the test does return a value, then it is set as the last value
in the values attribute of the result of the test.
1 |
|
Using return
The most convenient way a decorated test can return a value is by
using return statement. For example, a test step can be defined as follows:
1 |
|
and when called within another step, the returned value is received just like from a function.
1 | with Step("my step"): |
This is because calling a decorated test within a test of the same type, just runs the decorated test function and therefore the call is similar to calling a function with the ability to get the return value directly. See Calling Decorated Tests.
However, if you call a decorated test as a function
within a higher test type, for example calling a Step within a Test,
or when you call an inline defined test, then the return value
is a TestBase object and the returned value needs to be retrieved
as value attribute from the result attribute of the TestBase object,
or using values attribute to get a list of all the values produced by a test.
1 | with Test("my test"): |