Test
Test CLI.
Functions⚓︎
check ⚓︎
check(_ctx)
Run pytest checks, such as identifying.
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
if duplicate tests |
Source code in calcipy/tasks/test.py
@task()
def check(_ctx: Context) -> None:
"""Run pytest checks, such as identifying.
Raises:
RuntimeError: if duplicate tests
"""
if duplciates := check_duplicate_test_names.run(Path('tests')):
raise RuntimeError(f'Duplicate test names found ({duplciates}). See above for details.') # noqa: EM102
coverage ⚓︎
coverage(ctx, *, min_cover=0, out_dir=None, view=False)
Generate useful coverage outputs after running pytest.
Creates coverage.json used in doc.build
Source code in calcipy/tasks/test.py
@task(
help={
'min_cover': 'Fail if coverage less than threshold',
'out_dir': 'Optional path to coverage directory. Typically ".cover" or "releases/tests"',
'view': 'If True, open the created files',
},
)
def coverage(ctx: Context, *, min_cover: int = 0, out_dir: Optional[str] = None, view: bool = False) -> None:
"""Generate useful coverage outputs after running pytest.
Creates `coverage.json` used in `doc.build`
"""
pkg_name = read_package_name()
run(ctx, f'{python_m()} coverage run --branch --source={pkg_name} --module pytest ./tests')
cov_dir = Path(out_dir or from_ctx(ctx, 'test', 'out_dir'))
cov_dir.mkdir(exist_ok=True, parents=True)
print() # noqa: T201
fail_under = f' --fail-under={min_cover}' if min_cover > 0 else ''
for cli_args in (
f'report --show-missing{fail_under}', # Write to STDOUT
f'html --directory={cov_dir}', # Write to HTML
'json', # Create coverage.json file for "_handle_coverage"
):
run(ctx, f'{python_m()} coverage {cli_args}')
if view: # pragma: no cover
open_in_browser(cov_dir / 'index.html')
pytest ⚓︎
pytest(ctx, *, keyword='', marker='', min_cover=0)
Run pytest with default arguments.
Additional arguments can be set in the environment variable ‘PYTEST_ADDOPTS’
Source code in calcipy/tasks/test.py
@task(
default=True,
help={
'min_cover': 'Fail if coverage less than threshold',
**KM_HELP,
},
)
def pytest(ctx: Context, *, keyword: str = '', marker: str = '', min_cover: int = 0) -> None:
"""Run pytest with default arguments.
Additional arguments can be set in the environment variable 'PYTEST_ADDOPTS'
"""
pkg_name = read_package_name()
durations = '--durations=25 --durations-min="0.1"'
_inner_task(
ctx,
cli_args=f' --cov={pkg_name} --cov-branch --cov-report=term-missing {durations}',
keyword=keyword,
marker=marker,
min_cover=min_cover,
)
watch ⚓︎
watch(ctx, *, keyword='', marker='')
Run pytest with polling and optimized to stop on first error.
Source code in calcipy/tasks/test.py
@task(help=KM_HELP)
def watch(ctx: Context, *, keyword: str = '', marker: str = '') -> None:
"""Run pytest with polling and optimized to stop on first error."""
_inner_task(
ctx,
cli_args=' --failed-first --new-first --exitfirst -vv --no-cov',
keyword=keyword,
marker=marker,
command='ptw . --now',
run_as_module=False,
)