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."""