From c2bfe2e2c716c492111ea503260398b8c5cc479a Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 5 Oct 2020 20:05:23 -0600 Subject: [PATCH] Add invoke task to run tests This way, instead of needing to know that you run "pytest" and knowing tricks like "pytest -m ''" to run webtests and HTML validation, you can now just run "invoke test", with more intuitive flags. This also reduces the output in quiet mode even more. After adding invoke tasks for some of the other tools/checks, I'll be able to switch the git hooks to use these instead. --- tildes/pytest.ini | 2 +- tildes/tasks.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tildes/pytest.ini b/tildes/pytest.ini index 4e2516a..ededd1e 100644 --- a/tildes/pytest.ini +++ b/tildes/pytest.ini @@ -1,6 +1,6 @@ [pytest] testpaths = tests -addopts = -p no:cacheprovider --strict-markers -m "not (html_validation or webtest)" +addopts = -p no:cacheprovider --strict-markers filterwarnings = ignore::DeprecationWarning ignore::PendingDeprecationWarning diff --git a/tildes/tasks.py b/tildes/tasks.py index 314858d..2b94377 100644 --- a/tildes/tasks.py +++ b/tildes/tasks.py @@ -9,6 +9,53 @@ from pathlib import Path from invoke import task +def output(string): + """Output a string without a line ending and flush immediately.""" + print(string, end="", flush=True) + + +@task( + help={ + "html-validation": "Include HTML validation (very slow, includes webtests)", + "quiet": "Reduce verbosity", + "webtests": "Include webtests (a little slow)", + } +) +def test(context, quiet=False, webtests=False, html_validation=False): + """Run the tests. + + By default, webtests (ones that make actual HTTP requests to the app) and HTML + validation tests (checking the validity of the HTML on some of the site's pages) are + not run because they are slow, but you can include them with the appropriate flag. + """ + # webtests are required as part of HTML validation + if html_validation: + webtests = True + + pytest_args = [] + excluded_markers = [] + + if not webtests: + excluded_markers.append("webtest") + if not html_validation: + excluded_markers.append("html_validation") + + if excluded_markers: + excluded_marker_str = " or ".join(excluded_markers) + pytest_args.append(f'-m "not ({excluded_marker_str})"') + + if quiet: + output("Running tests... ") + + pytest_args.append("-q") + result = context.run("pytest " + " ".join(pytest_args), hide=True) + + # only output the final line of pytest's stdout (test count + runtime) + print(result.stdout.splitlines()[-1]) + else: + context.run("pytest " + " ".join(pytest_args), pty=True) + + @task def update_pip_requirements(context): """Use pip-tools to update package versions in the requirements files."""