| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Invoke test tasks. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import print_function |
| 7 |
import os.path |
| 8 |
import sys |
| 9 |
from invoke import task, Collection |
| 10 |
|
| 11 |
# -- TASK-LIBRARY: |
| 12 |
from ._tasklet_cleanup import cleanup_tasks, cleanup_dirs, cleanup_files |
| 13 |
|
| 14 |
|
| 15 |
# --------------------------------------------------------------------------- |
| 16 |
# TASKS |
| 17 |
# --------------------------------------------------------------------------- |
| 18 |
@task(name="all", help={ |
| 19 |
"args": "Command line args for test run.", |
| 20 |
}) |
| 21 |
def test_all(ctx, args="", options=""): |
| 22 |
"""Run all tests (default).""" |
| 23 |
pytest_args = select_by_prefix(args, ctx.pytest.scopes) |
| 24 |
behave_args = select_by_prefix(args, ctx.behave_test.scopes) |
| 25 |
pytest_should_run = not args or (args and pytest_args) |
| 26 |
behave_should_run = not args or (args and behave_args) |
| 27 |
if pytest_should_run: |
| 28 |
pytest(ctx, pytest_args, options=options) |
| 29 |
if behave_should_run: |
| 30 |
behave(ctx, behave_args, options=options) |
| 31 |
|
| 32 |
|
| 33 |
@task |
| 34 |
def clean(ctx, dry_run=False): |
| 35 |
"""Cleanup (temporary) test artifacts.""" |
| 36 |
directories = ctx.test.clean.directories or [] |
| 37 |
files = ctx.test.clean.files or [] |
| 38 |
cleanup_dirs(directories, dry_run=dry_run) |
| 39 |
cleanup_files(files, dry_run=dry_run) |
| 40 |
|
| 41 |
|
| 42 |
@task(name="unit") |
| 43 |
def unittest(ctx, args="", options=""): |
| 44 |
"""Run unit tests.""" |
| 45 |
pytest(ctx, args, options) |
| 46 |
|
| 47 |
|
| 48 |
@task |
| 49 |
def pytest(ctx, args="", options=""): |
| 50 |
"""Run unit tests.""" |
| 51 |
args = args or ctx.pytest.args |
| 52 |
options = options or ctx.pytest.options |
| 53 |
ctx.run("pytest {options} {args}".format(options=options, args=args)) |
| 54 |
|
| 55 |
|
| 56 |
@task(help={ |
| 57 |
"args": "Command line args for behave", |
| 58 |
"format": "Formatter to use (progress, pretty, ...)", |
| 59 |
}) |
| 60 |
# pylint: disable=redefined-builtin |
| 61 |
def behave(ctx, args="", format="", options=""): |
| 62 |
"""Run behave tests.""" |
| 63 |
format = format or ctx.behave_test.format |
| 64 |
options = options or ctx.behave_test.options |
| 65 |
args = args or ctx.behave_test.args |
| 66 |
if os.path.exists("bin/behave"): |
| 67 |
behave_cmd = "{python} bin/behave".format(python=sys.executable) |
| 68 |
else: |
| 69 |
behave_cmd = "{python} -m behave".format(python=sys.executable) |
| 70 |
|
| 71 |
for group_args in grouped_by_prefix(args, ctx.behave_test.scopes): |
| 72 |
ctx.run("{behave} -f {format} {options} {args}".format( |
| 73 |
behave=behave_cmd, format=format, options=options, args=group_args)) |
| 74 |
|
| 75 |
|
| 76 |
@task(help={ |
| 77 |
"args": "Tests to run (empty: all)", |
| 78 |
"report": "Coverage report format to use (report, html, xml)", |
| 79 |
}) |
| 80 |
def coverage(ctx, args="", report="report", append=False): |
| 81 |
"""Determine test coverage (run pytest, behave)""" |
| 82 |
append = append or ctx.coverage.append |
| 83 |
report_formats = ctx.coverage.report_formats or [] |
| 84 |
if report not in report_formats: |
| 85 |
report_formats.insert(0, report) |
| 86 |
opts = [] |
| 87 |
if append: |
| 88 |
opts.append("--append") |
| 89 |
|
| 90 |
pytest_args = select_by_prefix(args, ctx.pytest.scopes) |
| 91 |
behave_args = select_by_prefix(args, ctx.behave_test.scopes) |
| 92 |
pytest_should_run = not args or (args and pytest_args) |
| 93 |
behave_should_run = not args or (args and behave_args) |
| 94 |
if not args: |
| 95 |
behave_args = ctx.behave_test.args or "features" |
| 96 |
if isinstance(pytest_args, list): |
| 97 |
pytest_args = " ".join(pytest_args) |
| 98 |
if isinstance(behave_args, list): |
| 99 |
behave_args = " ".join(behave_args) |
| 100 |
|
| 101 |
# -- RUN TESTS WITH COVERAGE: |
| 102 |
if pytest_should_run: |
| 103 |
ctx.run("coverage run {options} -m pytest {args}".format( |
| 104 |
args=pytest_args, options=" ".join(opts))) |
| 105 |
if behave_should_run: |
| 106 |
behave_options = ctx.behave_test.coverage_options or "" |
| 107 |
os.environ["COVERAGE_PROCESS_START"] = os.path.abspath(".coveragerc") |
| 108 |
behave(ctx, args=behave_args, options=behave_options) |
| 109 |
del os.environ["COVERAGE_PROCESS_START"] |
| 110 |
|
| 111 |
# -- POST-PROCESSING: |
| 112 |
ctx.run("coverage combine") |
| 113 |
for report_format in report_formats: |
| 114 |
ctx.run("coverage {report_format}".format(report_format=report_format)) |
| 115 |
|
| 116 |
|
| 117 |
# --------------------------------------------------------------------------- |
| 118 |
# UTILITIES: |
| 119 |
# --------------------------------------------------------------------------- |
| 120 |
def select_prefix_for(arg, prefixes): |
| 121 |
for prefix in prefixes: |
| 122 |
if arg.startswith(prefix): |
| 123 |
return prefix |
| 124 |
return os.path.dirname(arg) |
| 125 |
|
| 126 |
def select_by_prefix(args, prefixes): |
| 127 |
selected = [] |
| 128 |
for arg in args.strip().split(): |
| 129 |
assert not arg.startswith("-"), "REQUIRE: arg, not options" |
| 130 |
scope = select_prefix_for(arg, prefixes) |
| 131 |
if scope: |
| 132 |
selected.append(arg) |
| 133 |
return " ".join(selected) |
| 134 |
|
| 135 |
def grouped_by_prefix(args, prefixes): |
| 136 |
"""Group behave args by (directory) scope into multiple test-runs.""" |
| 137 |
group_args = [] |
| 138 |
current_scope = None |
| 139 |
for arg in args.strip().split(): |
| 140 |
assert not arg.startswith("-"), "REQUIRE: arg, not options" |
| 141 |
scope = select_prefix_for(arg, prefixes) |
| 142 |
if scope != current_scope: |
| 143 |
if group_args: |
| 144 |
# -- DETECTED GROUP-END: |
| 145 |
yield " ".join(group_args) |
| 146 |
group_args = [] |
| 147 |
current_scope = scope |
| 148 |
group_args.append(arg) |
| 149 |
if group_args: |
| 150 |
yield " ".join(group_args) |
| 151 |
|
| 152 |
|
| 153 |
# --------------------------------------------------------------------------- |
| 154 |
# TASK MANAGEMENT / CONFIGURATION |
| 155 |
# --------------------------------------------------------------------------- |
| 156 |
namespace = Collection(clean, unittest, pytest, behave, coverage) |
| 157 |
namespace.add_task(test_all, default=True) |
| 158 |
namespace.configure({ |
| 159 |
"test": { |
| 160 |
"clean": { |
| 161 |
"directories": [ |
| 162 |
".cache", "assets", # -- TEST RUNS |
| 163 |
"__WORKDIR__", "reports", "test_results", # -- BEHAVE test |
| 164 |
], |
| 165 |
"files": [ |
| 166 |
".coverage", ".coverage.*", |
| 167 |
"report.html", |
| 168 |
"rerun*.txt", "rerun*.featureset", "testrun*.json", |
| 169 |
], |
| 170 |
}, |
| 171 |
}, |
| 172 |
"pytest": { |
| 173 |
"scopes": ["test", "tests"], |
| 174 |
"args": "", |
| 175 |
"options": "", # -- NOTE: Overide in configfile "invoke.yaml" |
| 176 |
}, |
| 177 |
# "behave_test": behave.namespace._configuration["behave_test"], |
| 178 |
"behave_test": { |
| 179 |
"scopes": ["features", "issue.features"], |
| 180 |
"args": "features issue.features", |
| 181 |
"format": "progress", |
| 182 |
"options": "", # -- NOTE: Overide in configfile "invoke.yaml" |
| 183 |
"coverage_options": "", |
| 184 |
}, |
| 185 |
"coverage": { |
| 186 |
"append": False, |
| 187 |
"report_formats": ["report", "html"], |
| 188 |
}, |
| 189 |
}) |
| 190 |
|
| 191 |
# -- ADD CLEANUP TASK: |
| 192 |
cleanup_tasks.add_task(clean, "clean_test") |
| 193 |
cleanup_tasks.configure(namespace.configuration()) |