pytest_cov_helper#

aws_ops_alpha.vendor.pytest_cov_helper.temp_cwd(path: Path)[source]#

Temporarily set the current working directory (CWD) and automatically switch back when it’s done.

aws_ops_alpha.vendor.pytest_cov_helper.run_cov_test(script: str, module: str, root_dir: str, htmlcov_dir: str, preview: bool = False, is_folder: bool = False)[source]#

The pytest-cov plugin gives you the coverage for entire project. What if I want run per-module test independently and get per-module coverage?

This is a simple wrapper around pytest + coverage cli command. Allow you to run coverage test from Python script and set the code coverage measurement scope.

Usage example:

suppose you have a source code folder structure like this:

/dir_git_repo/
/dir_git_repo/my_library
/dir_git_repo/my_library/__init__.py
/dir_git_repo/my_library/module1.py
/dir_git_repo/my_library/module2.py

In your module 1 unit test script, you can do this:

from my_library.module1 import func1, func2

def test_func1():
    pass

def test_func2():
    pass

if __name__ == "__main__":
    from fixa.pytest_cov_helper import run_cov_test

    run_cov_test(
        script=__file__,
        module="my_library.module1", # test scope is the module1.py
        root_dir="/path/to/dir_git_repo",
        htmlcov_dir="/path/to/dir_git_repo/htmlcov",
    )

In your all modules unit test script, you can do this:

if __name__ == "__main__":
    from fixa.pytest_cov_helper import run_cov_test

    run_cov_test(
        script=__file__,
        module="my_library", # test scope is the my_library/
        root_dir="/path/to/dir_git_repo",
        htmlcov_dir="/path/to/dir_git_repo/htmlcov",
        is_folder=True, # my_library is a folder
    )
Parameters:
  • script – the path to test script

  • module – the dot notation to the python module you want to calculate coverage

  • root_dir – the dir to dump coverage results binary file

  • htmlcov_dir – the dir to dump HTML output

  • preview – whether to open the HTML output in web browser after the test

  • is_folder – whether the module is a folder

Reference: