Generating the base case#

A single test file can generate multiple parameter-specific test cases. Optionally, a non-parameterized “base” case can be generated that depends on each of the parameterized cases by calling canary.directives.generate_composite_base_case():

# Copyright NTESS. See COPYRIGHT file for details.
#
# SPDX-License-Identifier: MIT

import os
import sys

import canary

canary.directives.parameterize("a", [1, 2, 3])
canary.directives.generate_composite_base_case()


def run_parameterized_case(case: canary.TestInstance) -> None:
    # Run the test
    f = f"{case.parameters.a}.txt"
    canary.filesystem.touchp(f)


def analyze_composite_base_case(case: canary.TestMultiInstance) -> None:
    # Analyze the collective
    assert len(case.dependencies) == 3
    for dep in case.dependencies:
        f = os.path.join(dep.working_directory, f"{dep.parameters.a}.txt")
        assert os.path.exists(f)


def main():
    self = canary.get_instance()
    if isinstance(self, canary.TestMultiInstance):
        analyze_composite_base_case(self)
    else:
        run_parameterized_case(self)
    return 0


if __name__ == "__main__":
    sys.exit(main())

In this example, the base case that depends on test cases having a=1, a=2, and a=3, is automatically generated:

$ canary describe execute_and_analyze/execute_and_analyze.pyt
--- execute_and_analyze ------------
File: /home/docs/checkouts/readthedocs.org/user_builds/canary-wm/checkouts/latest/src/canary/examples/execute_and_analyze/execute_and_analyze.pyt
Keywords: 
4 test specs using on_options=:
├── execute_and_analyze.a=1
├── execute_and_analyze.a=2
├── execute_and_analyze.a=3
└── execute_and_analyze
│   ├── execute_and_analyze.a=1
│   ├── execute_and_analyze.a=2
│   └── execute_and_analyze.a=3

The base will not run until each parameterized case is run.