From ddfb5eb8386201105d5890260507663b7eed50da Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Sun, 20 Jun 2021 11:45:52 +0200 Subject: [PATCH] qa: convert test_import.py to pytest Signed-off-by: Sebastian Wagner --- qa/CMakeLists.txt | 2 +- qa/test_import.py | 25 ++++++++++--------------- qa/tox.ini | 12 +++++------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/qa/CMakeLists.txt b/qa/CMakeLists.txt index a72f9088408..884c41e4bba 100644 --- a/qa/CMakeLists.txt +++ b/qa/CMakeLists.txt @@ -5,5 +5,5 @@ endif() if(WITH_TESTS) include(AddCephTest) - add_tox_test(qa TOX_ENVS py3 flake8 import-tasks mypy) + add_tox_test(qa TOX_ENVS py3 flake8 mypy) endif() diff --git a/qa/test_import.py b/qa/test_import.py index fe70a1c4980..4ee59b56509 100644 --- a/qa/test_import.py +++ b/qa/test_import.py @@ -1,11 +1,10 @@ # try to import all .py files from a given directory -import argparse import glob import os import importlib import importlib.util - +import pytest def _module_name(path): task = os.path.splitext(path)[0] @@ -19,25 +18,21 @@ def _import_file(path): line = f'Importing {package}{mod_name} from {path}' print(f'{line:<80}', end='') mod_spec = importlib.util.find_spec(mod_name, package) - mod = mod_spec.loader.load_module() + mod = mod_spec.loader.load_module(f'{package}{mod_name}') if mod is None: result = 'FAIL' else: result = 'DONE' print(f'{result:>6}') mod_spec.loader.exec_module(mod) + return result -def _parser(): - parser = argparse.ArgumentParser( - description='Try to import a file', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument('path', nargs='+', help='Glob to select files') - return parser +def get_paths(): + for g in ['tasks/**/*.py']: + for p in glob.glob(g, recursive=True): + yield p +@pytest.mark.parametrize("path", list(sorted(get_paths()))) +def test_import(path): + assert _import_file(path) == 'DONE' -if __name__ == '__main__': - parser = _parser() - args = parser.parse_args() - for g in args.path: - for p in glob.glob(g, recursive=True): - _import_file(p) diff --git a/qa/tox.ini b/qa/tox.ini index b5e4b2a647a..1046597ac16 100644 --- a/qa/tox.ini +++ b/qa/tox.ini @@ -22,14 +22,12 @@ deps = -c{toxinidir}/../src/mypy-constrains.txt commands = mypy {posargs:.} -[testenv:import-tasks] -basepython = python3 -deps = {env:TEUTHOLOGY_GIT:git+https://github.com/ceph/teuthology.git@master}#egg=teuthology[coverage,orchestra,test] -commands = python test_import.py {posargs:tasks/**/*.py} - [testenv:py3] basepython = python3 deps = - {env:TEUTHOLOGY_GIT:git+https://github.com/ceph/teuthology.git@master}#egg=teuthology[test] + {env:TEUTHOLOGY_GIT:git+https://github.com/ceph/teuthology.git@master}#egg=teuthology[coverage,orchestra,test] httplib2 -commands = pytest -vv tasks/tests +commands = + pytest --assert=plain test_import.py + pytest tasks/tests + -- 2.39.5