Test order control
Test Order Control
The control of the Test Order allows you to precisely define the order of test execution. TestFlows allows you to write complete test programs, and therefore the order of executed tests is defined in your Python test program code explicitly.
For example, the following test program defines decorated tests
testA, testB, and testC which are executed in the regression() module
in the testA -> testB -> testC order.
1 | from testflows.core import * |
It is trivial to see that given that the order of test execution (Test Order) is explicitly
defined in regression() we could easily change it from testA -> testB -> testC to
testC -> testA -> testB.
1 |
|
Conditional Test Execution
Conditional execution can be added to any explicitly defined Test Order using standard Python Flow Control Tools using if, while, and for statements.
For example,
1 |
|
will execute testA and only proceed to run other tests if its result is not Fail otherwise
only testA will be executed. If result of testA is not Fail then
we run testB 3 times, and testC gets executed indefinitely until its result is OK.
Ordering Tests Automatically
When precise control over the Test Order is not necessary, you can easily define a list of tests to be executed in any way you might see fit, including using a simple list.
For example,
1 | # list of all tests |
For such simple cases, you can also use loads() function. See Using loads().
The loads() function allows you to create a list of tests of the specified type from either the current or some other module.
For example,
1 |
|
Here is an example of loading tests from my_project/tests.py module,
1 |
|
The list of tests can be randomized or ordered, for example, using ordered() function or Python‘s sorted function.
✋ You could also write Python code to load your list of tests from any other source such as a file system, database, or API endpoint, etc.
Setting Test Results Explicitly
A result of any test can be set explicitly using the following result functions:
- fail() function for Fail
- err() function for Error
- skip() function for Skip
- null() function for Null
- ok() function for OK
- xfail() function for XFail
- xerr() function for XError
- xnull() function for XNull
- xok() function for XOK
Here are the arguments that each result function can take. All arguments are optional.
messageis used to set an optional result messagereasonis used to set an optional reason for the result. Usually, it is only set for crossed out results such as XFail, XError, XNull and XOK to indicate the reason for the result being crossed out such as a link to an opened issuetestargument is usually not passed, as it is set to the current test by default. See current() function.
1 | ok(message=None, reason=None, test=None) |
These functions raise an exception that corresponds to the appropriate result class and therefore, unless you explicitly catch the exception, the test stops at the point at which the result function is called.
For example,
1 | from testflows.core import * |
You can also raise the result explicitly.
For example,
1 | from testflows.core import * |
Fails of Specific Types
TestFlows does not support adding types to the Fails but
the fail() function takes an optional type argument that takes
one of the Test Definition Classes which will be used to create a sub-test
with the name specified by the message and failed with the specified
reason.
The original use case is to provide a way to separate fails of Checks into Critical, Major and Minor without explicitly defining Critical, Major, or Minor sub-tests.
For example,
1 | from testflows.core import * |
The above code is equivalent to the following.
1 | from testflows.core import * |