Launching TestFlows Machine: self-service deterministic simulation testing environment Read the post

Test Definition Classes

Module

A Module can be defined using Module test definition class or TestModule decorator.

1
2
3
@TestModule
def module(self):
Feature(run=feature)

or inline as

1
2
with Module("module"):
Feature(run=feature)

Suite

A Suite can be defined using Suite test definition class or TestSuite decorator.

1
2
3
4
@TestSuite
@Name("My suite")
def suite(self):
Test(run=testcase)

or inline as

1
2
with Suite("My suite"):
Test(run=testcase)

Feature

A Feature can be defined using Feature test definition class or TestFeature decorator.

1
2
3
4
@TestFeature
@Name("My feature")
def feature(self):
Scenario(run=scenario)

or inline as

1
2
with Feature("My feature"):
Scenario(run=scenario)

Test

A Case can be defined using Test test definition class or TestCase decorator.

1
2
3
4
5
@TestCase
@Name("My testcase")
def testcase(self):
with Step("do something"):
pass

or inline as

1
2
3
with Test("My testcase"):
with Step("do something"):
pass

✋ Note that here the word test is used to define a Case to match the most common meaning of the word test. When someone says they will run a test they most likely mean they will run a test Case.

Scenario

A Scenario can be defined using Scenario test definition class or TestScenario decorator.

1
2
3
4
@TestScenario
@Name("My scenario"):
def scenario(self):
pass

or inline as

1
2
with Scenario("My scenario"):
pass

Check

A Check can be defined using Check test definition class or TestCheck decorator

1
2
3
4
@TestCheck
@Name("My check")
def check(self):
pass

or inline as

1
2
with Check("My check"):
pass

and is usually used inside either Test or Scenario to define an inline sub-test

1
2
3
4
5
6
with Scenario("My scenario"):
with Check("My check"):
pass

with Check("My other check"):
pass

Critical, Major, Minor

A Critical, Major, or Minor checks can be defined using Critical, Major or Minor test definition class, respectively, or similarly using TestCritical, TestMajor, TestMinor decorators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@TestCritical
@Name("My critical check")
def critical(self):
pass

@TestMajor
@Name("My major check")
def major(self):
pass

@TestMinor
@Name("My minor minor")
def minor(self):
pass

or inline as

1
2
3
4
5
6
7
8
with Critical("My critical check"):
pass

with Major("My major check"):
pass

with Minor("My minor check"):
pass

and are usually used inside either Test or Scenario to define inline sub-tests.

1
2
3
4
5
6
7
8
9
with Scenario("My scenario"):
with Critical("my critical check"):
pass

with Major("my major check"):
pass

with Minor("my minor check"):
pass

These classes are usually used for the classification of checks during reporting.

1
2
3
4
1 scenario (1 ok)
1 critical (1 ok)
1 major (1 ok)
1 minor (1 ok)

Example

An Example can only be defined inline using Example test definition class. There is no decorator to define it outside of existing test. An Example is a sub-type of a Test Type and is used to define one or more sub-tests. Usually, Examples are created automatically using Outlines.

1
2
3
4
5
6
with Scenario("My scenario"):
with Example("Example 1"):
pass

with Example("Example 2"):
pass

Outline

An Outline can be defined using Outline test definition class or TestOutline decorator. An Outline is a sub-type of a Test type but you can change the type by passing it another Type or a Sub-Type such as Scenario or Suite etc.

However, because Outlines are meant to be called from other tests or used with Examples it is best to define an Outline using TestOutline decorator as follows.

1
2
3
4
5
6
7
8
9
10
11
from testflows.core import *

@TestOutline(Scenario)
@Examples("greeting name", [
("Hello", "John"),
("Goodbye", "Eric")
])
def outline(self, greeting, name):
note(f"{greeting} {name}!")

outline()

When Examples are defined for the Outline and an outline is called with no arguments from a test that is of a higher Type than the Type of outline itself, then when called, the outline will iterate over all the examples defined in the Examples table. For example, if you run the example above that executes the outline with no arguments, you will see that the outline iterates over all the examples in the Examples table, where each example, a row in the examples table, defines the values of the arguments for the outline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Jul 05,2020 18:16:34   ⟥  Scenario outline
Examples
greeting | name
-------- | ----
Hello | John
Goodbye | Eric
Jul 05,2020 18:16:34 ⟥ Example greeting='Hello', name='John'
Arguments
greeting
Hello
name
John
757us ⟥ [note] Hello John!
868us ⟥⟤ OK greeting='Hello', name='John', /outline/greeting='Hello', name='John'
Jul 05,2020 18:16:34 ⟥ Example greeting='Goodbye', name='Eric'
Arguments
greeting
Goodbye
name
Eric
675us ⟥ [note] Goodbye Eric!
766us ⟥⟤ OK greeting='Goodbye', name='Eric', /outline/greeting='Goodbye', name='Eric'
4ms ⟥⟤ OK outline, /outline

If we run the same outline with arguments, then the outline will not use the Examples but instead will use the argument values that were provided to the outline. For example,

1
2
with Scenario("My scenario"):
outline(greeting="Hey", name="Bob")

will produce the following output.

1
2
3
Jul 05,2020 18:23:02   ⟥  Scenario My scenario
1ms ⟥ [note] Hey Bob!
2ms ⟥⟤ OK My scenario, /My scenario

Combination

A Combination is not meant to be used explicitly, and in most cases it is only used internally to represent each combination of a Sketch.

Sketch

A Sketch is defined using the TestSketch decorator. In most cases you should not use Sketch test definition class directly as it will not execute its Combinations. A Sketch is a sub-type of a type Test but you can specify a different type. For example, you can pass a specific Type or a Sub-Type such as Scenario, Suite, or Feature etc. when defining a TestSketch.

Because Sketchs are designed to execute different Combinations, one for each Combination defined by the either() function, it is best to define a Sketch only using a TestSketch decorator.

✋ TestSketch is designed to work with either() function that is used to define combination variables and their possible values.

For example,

1
2
3
4
5
6
7
8
9
10
def add(a, b):
return a + b

@TestSketch(Scenario)
def my_sketch(self):
a = either(1,2)
b = either(2,3)
r = add(a, b)
note(f"{a} + {b} = {r}")
assert r == a + b

The TestSketch above calls the add() function with different combinations of its a and b parameters. The Sketch checks combinations when a argument is either 1 or 2, and b argument is either 2 or 3. Therefore, the following combination patterns are covered:

  • pattern #0: add(1,2)
  • pattern #1: add(1,3)
  • pattern #2: add(2,2)
  • pattern #3: add(2,3)

You can see this from the output of the test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sep 21,2023 13:42:38   ⟥  Scenario my sketch
Sep 21,2023 13:42:38 ⟥ Combination pattern #0
264us ⟥ [note] 1 + 2 = 3
332us ⟥⟤ OK pattern #0, /my sketch/pattern #0
Sep 21,2023 13:42:38 ⟥ Combination pattern #1
264us ⟥ [note] 1 + 3 = 4
324us ⟥⟤ OK pattern #1, /my sketch/pattern #1
Sep 21,2023 13:42:38 ⟥ Combination pattern #2
186us ⟥ [note] 2 + 2 = 4
236us ⟥⟤ OK pattern #2, /my sketch/pattern #2
Sep 21,2023 13:42:38 ⟥ Combination pattern #3
171us ⟥ [note] 2 + 3 = 5
218us ⟥⟤ OK pattern #3, /my sketch/pattern #3
4ms ⟥⟤ OK my sketch, /my sketch

See Using Sketches for more details.

Iteration

An Iteration is not meant to be used explicitly, and in most cases it is only used internally to implement test repetitions.

RetryIteration

A RetryIteration is not meant to be used explicitly and, in most cases, is only used internally to implement test retries.

Step

A Step can be defined using Step test definition class or TestStep decorator.

1
2
3
@TestStep
def step(self):
note("generic test step")

A TestStep can be made specific by passing it a specific [BBD] step Sub-Type.

1
2
3
@TestStep(When)
def step(self):
note("a When step")

A Step can be defined inline as

1
2
with Step("step"):
note("generic test step")

Given

A Given step is used to define preconditions or setup and is always treated as a mandatory step that can’t be skipped because MANDATORY flag will be set by default. It is defined using Given test definition class or using TestStep with Given passed as the Sub-Type.

1
2
3
@TestStep(Given)
def I_have_something(self):
pass

or inline as

1
2
with Given("I have something"):
pass

Background

A Background step is used to define a complex preconditions or setup, usually containing multiple Given‘s and can be defined using Background test definition class or TestBackground decorator. It is treated as a mandatory step that can’t be skipped.

1
2
3
4
5
6
7
8
@TestBackground
@Name("My complex setup")
def background(self):
with Given("I need to setup something"):
pass

with And("I need to setup something else"):
pass

or inline as

1
2
3
4
5
6
with Background("My complex setup"):
with Given("I need to setup something"):
pass

with And("I need to setup something else"):
pass

When

A When step is used to define an action within a Scenario. It can be defined using When test definition class or using TestStep decorator with When passed as the Sub-Type.

1
2
3
@TestStep(When)
def I_do_some_action(self):
do_some_action()

or inline as

1
2
with When("I do some action"):
do_some_action()

And

An And step is used to define a step of the same Sub-Type as the step right above it. It is defined using And test definition class.

✋ It does not make sense to use TestStep decorator to define it, so always define it inline.

1
2
3
4
5
with When("I do some action"):
pass

with And("I do another action"):
pass

or

1
2
3
4
5
with Given("I have something"):
with When("I do some action to setup things"):
pass
with And("I do another action to continue the setup"):
pass

✋ TypeError exception will be raised if the And step is defined where it has no siblings. For example,

1
2
3
4
5
with Given("I have something"):
# TypeError exception will be raised on the next line
# and can be fixed by changing the `And` step into a `When` step
with And("I do something"):
pass

with the exception being as follows.

1
TypeError: `And` subtype can't be used here as it has no sibling from which to inherit the subtype

✋ TypeError exception will also be raised if the Type of the sibling does not match the Type of the And step. For example,

1
2
3
4
5
6
7
with Scenario("My scenario"):
pass

# TypeError exception will be raised on the next line
# and can be fixed by changing the `And` step into a `When` step
with And("I do something"):
pass

with the exception being as follows.

1
TypeError: `And` subtype can't be used here as it sibling is not of the same type

By

A By step is usually used to define a sub-step using By test definition class.

1
2
3
with When("I do something"):
with By("doing some action"):
pass

Then

A Then step is used to define a step that usually contains a positive assertion. It can be defined using Then test definition class or using TestStep decorator with Then passed as the Sub-Type.

1
2
3
@TestStep(Then)
def I_check_something_is_true(self):
assert something

or inline as

1
2
with Then("I expect something"):
assert something

But

A companion of the Then step is a But step and is used to define a step that usually contains a negative assertion. It can be defined using But test definition class or using TestStep decorator with But passed as the Sub-Type.

1
2
3
@TestStep(But)
def I_check_something_is_not_true(self):
assert not something

or inline as

1
2
with But("I check something is not true"):
assert not something

Finally

A Finally step is used to define a cleanup step and is treated as a mandatory step that can’t be skipped because MANDATORY flag will be set by default.

It can be defined using Finally test definition class or using TestStep decorator with Finally passed as the Sub-Type.

1
2
3
@TestStep(Finally)
def I_clean_up(self):
pass

or inline as

1
2
with Finally("I clean up"):
pass

The TE flag is always set for Finally steps as multiple Finally steps can be defined back to back and the failure of a previous step should not prevent execution of other Finally steps that follow.