Defining tests
Defining Tests
You can define tests inline using the classical Step, Test, Suite, and Module test definition classes or using specialized keyword classes as Scenario, Feature, Module and the steps such as Background, Given, When, Then, But, By, And, and Finally.
In addition, you can also define sub-tests using Check test definition class or its flavours Critical, Major or Minor.
✋ You are encouraged to use the specialized keyword classes to greatly improve the readability of your tests and test procedures.
Given the variety of test definition classes above, fundamentally, there are only four core Types of tests in TestFlows. The core Types are
and all other types are just a naming variation of one of the above with the following mapping or are special types
- Module
- Suite
- Test
- Step
- Sketch (special)
- Combination (special)
- Outline (special)
- Iteration (special)
- RetryIteration (special)
see Types for more information.
Inline
Inline tests can be defined anywhere in your test program by using Test Definition Classes above. Because all test definition classes are context managers, therefore they must be used using the with statement or async with for asynchronous tests that leverage Python‘s asyncio module.
1 | with Module("My test module"): |
Decorated
For re-usability, you can also define tests using the TestStep, TestBackground, TestCase, TestCheck, TestCritical, TestMajor, TestMinor, TestSuite, TestFeature, TestModule, TestOutline, and TestSketch test function decorators.
For example,
1 |
|
Similarly to how class method‘s take an instance of the object as the first argument,
test functions wrapped with test decorators take an instance of the current test as the first argument
and therefore, by convention, the first argument is always named self.
Calling Decorated Tests
✋ All arguments to tests must be passed using keyword arguments.
For example,
1 | scenario(action="driving") |
Use a test definition class to run another test as
1 | Scenario(test=scenario)(action="running") |
where the test is passed as the argument to the test parameter.
If the test does not need any arguments, use a short form by passing
the test as the value of the run parameter.
1 | Scenario(run=scenario) |
✋ Use the short form only when you don’t need to pass any arguments to the test.
This will be equivalent to
1 | Scenario(test=scenario)() |
You can also call decorated tests directly as
1 | scenario(action="swimming") |
Note that scenario() call will only create its own Scenario if and only if it is
running within a parent that has a higher test Type such as Feature or Module.
However, if you call it within the same test Type then it will not create its own Scenario but will run simply as a function within the scope of the current test.
For example,
1 | with Scenario("My scenario"): |
will run in the scope of My scenario where self will be an instance of the
1 | Scenario("My scenario") |
but
1 | with Feature("My feature"): |
will create its own test.
Running Tests
Top level tests can be run using either python3 command or directly if they are made executable.
For example, with a top level test defined as
1 | from testflows.core import Test |
you can run it with python3 command as follows
1 | python3 test.py |
or we can make the top level test executable and defined as
1 | #!/usr/bin/python3 |
and then we can make it executable with
1 | chmod +x test.py |
allowing us to execute it directly as follows.
1 | ./test.py |