Subscenarios: reuse existing scenarios in other scenarios

Scenarios can be reused as subscenarios in other ones.

Executing existing scenarios as sub-scenarios are particularly useful for the following purposes:

  • define alternative scenarios (error scenarios) from a nominal one,

  • reuse a nominal scenario as the initial condition of other ones, in order to bring the system or software under test in the expected initial state,

  • repeat a base scenario with varying input data.

Initial conditions

Todo

Documentation needed for initial conditions.

Varying input data

Todo

Improve subscenario documentation with a better example.

In order to illustrate this use case of subscenarios, let’s get back to the previous CommutativeAddition scenario defined previously.

The CommutativeAddition scenario can be called multiple times, with different inputs, in a super CommutativeAdditions scenario:

 1# -*- coding: utf-8 -*-
 2
 3import pathlib
 4import scenario
 5import sys
 6
 7sys.path.append(str(pathlib.Path(__file__).parent))
 8from commutativeaddition import CommutativeAddition  # noqa: E402
 9
10
11class CommutativeAdditions(scenario.Scenario):
12
13    SHORT_TITLE = "Commutative additions"
14    TEST_GOAL = "Call the CommutativeAddition scenario with different inputs."
15
16    def step010(self):
17        self.STEP("Both positive members")
18
19        if self.ACTION("Launch the CommutativeAddition scenario with 4 and 5 for inputs."):
20            _scenario = CommutativeAddition(4, 5)
21            scenario.runner.executescenario(_scenario)
22
23    def step020(self):
24        self.STEP("Positive and negative members")
25
26        if self.ACTION("Launch the CommutativeAddition scenario with -1 and 3 for inputs."):
27            _scenario = CommutativeAddition(-1, 3)
28            scenario.runner.executescenario(_scenario)
29
30    def step030(self):
31        self.STEP("Both negative members")
32
33        if self.ACTION("Launch the CommutativeAddition scenario with -1 and -7 for inputs."):
34            _scenario = CommutativeAddition(-1, -7)
35            scenario.runner.executescenario(_scenario)

To do so, start with loading your base scenario with a regular import statement:

from commutativeaddition import CommutativeAddition  # noqa: E402

Instanciate it with the appropriate values:

            _scenario = CommutativeAddition(4, 5)

And execute it as a subscenario:

            scenario.runner.executescenario(_scenario)

Executing this super scenario produces the following output:

$ ./bin/run-test.py ./demo/commutativeadditions.py
SCENARIO 'demo/commutativeadditions.py'
------------------------------------------------


STEP#1: Both positive members (demo/commutativeadditions.py:16:CommutativeAdditions.step010)
------------------------------------------------
    ACTION: Launch the CommutativeAddition scenario with 4 and 5 for inputs.
      | SCENARIO 'demo/commutativeaddition.py'
      | ------------------------------------------------
      |
      | STEP#1: Initial conditions (demo/commutativeaddition.py:18:CommutativeAddition.step000)
      | ------------------------------------------------
      |     ACTION: Let a = 4, and b = 5
      |   EVIDENCE:   -> a = 4
      |   EVIDENCE:   -> b = 5
      |
      | STEP#2: a + b (demo/commutativeaddition.py:25:CommutativeAddition.step010)
      | ------------------------------------------------
      |     ACTION: Compute (a + b) and store the result as result1.
      |   EVIDENCE:   -> result1 = 9
      |
      | STEP#3: b + a (demo/commutativeaddition.py:32:CommutativeAddition.step020)
      | ------------------------------------------------
      |     ACTION: Compute (b + a) and store the result as result2.
      |   EVIDENCE:   -> result2 = 9
      |
      | STEP#4: Check (demo/commutativeaddition.py:39:CommutativeAddition.step030)
      | ------------------------------------------------
      |     ACTION: Compare result1 and result2.
      |     RESULT: result1 and result2 are the same.
      |   EVIDENCE:   -> 9 == 9
      |
      | END OF 'demo/commutativeaddition.py'

STEP#2: Positive and negative members (demo/commutativeadditions.py:23:CommutativeAdditions.step020)
------------------------------------------------
    ACTION: Launch the CommutativeAddition scenario with -1 and 3 for inputs.
      | SCENARIO 'demo/commutativeaddition.py'
      | ------------------------------------------------
      |
      | STEP#1: Initial conditions (demo/commutativeaddition.py:18:CommutativeAddition.step000)
      | ------------------------------------------------
      |     ACTION: Let a = -1, and b = 3
      |   EVIDENCE:   -> a = -1
      |   EVIDENCE:   -> b = 3
      |
      | STEP#2: a + b (demo/commutativeaddition.py:25:CommutativeAddition.step010)
      | ------------------------------------------------
      |     ACTION: Compute (a + b) and store the result as result1.
      |   EVIDENCE:   -> result1 = 2
      |
      | STEP#3: b + a (demo/commutativeaddition.py:32:CommutativeAddition.step020)
      | ------------------------------------------------
      |     ACTION: Compute (b + a) and store the result as result2.
      |   EVIDENCE:   -> result2 = 2
      |
      | STEP#4: Check (demo/commutativeaddition.py:39:CommutativeAddition.step030)
      | ------------------------------------------------
      |     ACTION: Compare result1 and result2.
      |     RESULT: result1 and result2 are the same.
      |   EVIDENCE:   -> 2 == 2
      |
      | END OF 'demo/commutativeaddition.py'

STEP#3: Both negative members (demo/commutativeadditions.py:30:CommutativeAdditions.step030)
------------------------------------------------
    ACTION: Launch the CommutativeAddition scenario with -1 and -7 for inputs.
      | SCENARIO 'demo/commutativeaddition.py'
      | ------------------------------------------------
      |
      | STEP#1: Initial conditions (demo/commutativeaddition.py:18:CommutativeAddition.step000)
      | ------------------------------------------------
      |     ACTION: Let a = -1, and b = -7
      |   EVIDENCE:   -> a = -1
      |   EVIDENCE:   -> b = -7
      |
      | STEP#2: a + b (demo/commutativeaddition.py:25:CommutativeAddition.step010)
      | ------------------------------------------------
      |     ACTION: Compute (a + b) and store the result as result1.
      |   EVIDENCE:   -> result1 = -8
      |
      | STEP#3: b + a (demo/commutativeaddition.py:32:CommutativeAddition.step020)
      | ------------------------------------------------
      |     ACTION: Compute (b + a) and store the result as result2.
      |   EVIDENCE:   -> result2 = -8
      |
      | STEP#4: Check (demo/commutativeaddition.py:39:CommutativeAddition.step030)
      | ------------------------------------------------
      |     ACTION: Compare result1 and result2.
      |     RESULT: result1 and result2 are the same.
      |   EVIDENCE:   -> -8 == -8
      |
      | END OF 'demo/commutativeaddition.py'

END OF 'demo/commutativeadditions.py'
------------------------------------------------
             Status: SUCCESS
    Number of STEPs: 3/3
  Number of ACTIONs: 3/3
  Number of RESULTs: 0/0
               Time: HH:MM:SS.mmmmmm

Each subscenario execution appears indented with a pipe character.

Subscenario nestings

A subscenario may call other subscenarios.

For each subscenario in the execution stack, a pipe indentation is inserted in the log lines, in order to highlight the scenario and subscenario execution nestings.