github.com/SUSE/skuba@v1.4.17/ci/infra/testrunner/tests/driver.py (about)

     1  import os
     2  
     3  import pytest
     4  
     5  FILEPATH = os.path.realpath(__file__)
     6  TESTRUNNER_DIR = os.path.dirname(os.path.dirname(FILEPATH))
     7  
     8  PYTEST_RC = {
     9      0: "all tests passed successfully",
    10      1: "some of the tests failed",
    11      2: "execution was interrupted by the user",
    12      3: "internal error happened while executing tests",
    13      4: "pytest command line usage error",
    14      5: "no tests were collected"
    15  }
    16  
    17  class TestDriver:
    18      def __init__(self, conf, platform):
    19          self.conf = conf
    20          self.platform = platform
    21  
    22      def run(self, module=None, test_suite=None,
    23              test=None, verbose=False, collect=False,
    24              skip_setup=None, mark=None, junit=None, traceback="short"):
    25          opts = []
    26  
    27          vars_opt = "--vars={}".format(self.conf.yaml_path)
    28          opts.append(vars_opt)
    29  
    30          platform_opt = "--platform={}".format(self.platform)
    31          opts.append(platform_opt)
    32  
    33          if verbose:
    34              opts.append("-s")
    35  
    36          # Dont capture logs
    37          opts.append("--show-capture=no")
    38  
    39          # generete detailed test results
    40          opts.append("-v")
    41  
    42          if collect:
    43              opts.append("--collect-only")
    44  
    45          if skip_setup is not None:
    46              opts.append(f"--skip-setup={skip_setup}")
    47  
    48          if junit is not None:
    49              opts.append(f"--junitxml={TESTRUNNER_DIR}/{junit}.xml")
    50  
    51          if mark is not None:
    52              opts.append(f'-m {mark}')
    53  
    54          opts.append(f'--tb={traceback}')
    55  
    56          test_path = module if module is not None else "tests"
    57  
    58          if test_suite:
    59              if not test_suite.endswith(".py"):
    60                  raise ValueError("Test suite must be a python file")
    61              test_path = os.path.join(test_path, test_suite)
    62  
    63          if test:
    64              if not test_suite:
    65                  raise ValueError("Test suite is required for selecting a test")
    66              test_path = "{}::{}".format(test_path, test)
    67  
    68          # Path must be the last argument
    69          opts.append(test_path)
    70  
    71          # Before running the tests, switch to the directory of the testrunner.py
    72          os.chdir(TESTRUNNER_DIR)
    73  
    74          result = pytest.main(args=opts)
    75  
    76          if result in [0, 1]:
    77              raise SystemExit(result)
    78  
    79          if result in [2, 3, 4, 5]:
    80              raise Exception(f'error executing test {PYTEST_RC[result]}')
    81  
    82          raise Exception(f'unexpected return code from pytest {result}')