diff --git a/src/actions/pytest.py b/src/actions/pytest.py new file mode 100644 index 0000000..f916148 --- /dev/null +++ b/src/actions/pytest.py @@ -0,0 +1,47 @@ +"""Run unit tests.""" + +# standard library +import os +import subprocess + +# third party +import pytest + +# first party +import delphi.github_deploy_repo.file_operations as file_operations + +class Plugin: + def __init__(self): + self.summary={ + "passed":0, + "failed":0, + "skipped":0, + "errors":0 + } + def pytest_runtest_logreport(self, report): + self.summary[report.outcome] += 1 + def pytest_internalerror(self, *args, **kwargs): + self.summary["errors"] += 1 + def pytest_exception_interact(self, *args, **kwargs): + self.summary["errors"] += 1 + +def pytest(repo_link, commit, path, row, substitutions): + # pytest [dir] + + # parse arguments + if 'dir' in row: + location = file_operations.get_file(row['dir'], path, substitutions)[0] + else: + location = os.path.join(path, 'tests') + pattern = '^(test_.*|.*_test)\\.py$' + + # run tests and gather results + p = Plugin() + pytest.main([location, 'no:terminal'], plugins=[p]) + bad = p.summary['failed'] + p.summary['errors'] + if bad > 0: + raise Exception('%d test(s) did not pass' % bad) + elif p.summary['passed'] == 0: + print('no tests found') + else: + print('%d test(s) passed!' % p.summary['passed']) diff --git a/src/github_deploy_repo.py b/src/github_deploy_repo.py index 10cdf05..ab2957f 100644 --- a/src/github_deploy_repo.py +++ b/src/github_deploy_repo.py @@ -34,6 +34,7 @@ from delphi.github_deploy_repo.actions.copymove import copymove from delphi.github_deploy_repo.actions.minimize_js import minimize_js from delphi.github_deploy_repo.actions.py3test import py3test +from delphi.github_deploy_repo.actions.pytest import pytest import delphi.github_deploy_repo.database as database import delphi.operations.secrets as secrets import delphi.utils.extractor as extractor @@ -111,6 +112,7 @@ def execute(repo_link, commit, path, config): 'compile-coffee': compile_coffee, 'minimize-js': minimize_js, 'py3test': py3test, + 'pytest': pytest } for (idx, row) in enumerate(actions): # each row should be either: a map/dict/object with a string field named