mypythontools.tests module

mypythontools.tests.deactivate_test_settings()[source]

Sometimes you want to run test just in normal mode (enable plots etc.). Usually at the end of test file in if __name__ = “__main__”: block.

mypythontools.tests.run_tests(test_path=None, test_coverage=True, stop_on_first_error=True, use_virutalenv=True, requirements=None, verbose=False, extra_args=[])[source]

Run tests. If any test fails, raise an error.

Parameters
  • test_path ((str, pathlib.Path), optional) – If None, root is used. root is necessary if using doctests ‘Tests’ folder not works for doctests in modules. Defaults to None.

  • test_coverage (bool, optional) – Whether run test coverage plugin. If True, pytest-cov must be installed. Defaults to True

  • stop_on_first_error (bool, optional) – Whether stop on first error. Defaults to True

  • use_virutalenv (bool, optional) – Whether run new virtualenv and install all libraries from requirements.txt. Defaults to True

  • requirements ((str, pathlib.Path, list), optional) – If using use_virutalenv define what libraries will be installed. If None, autodetected. Can also be a list of more files e.g [“requirements.txt”, “requirements_dev.txt”]. Defaults to None.

  • verbose (bool, optional) – Whether print detailes on errors or keep silent. If False, parameters -q and `–tb=no are added. Defaults to False.

  • extra_args (list, optional) – List of args passed to pytest. Defaults to []

Raises

Exception – If any test fail, it will raise exception (git hook do not continue…).

Note

If not using with utils.project_utils_pipeline, paths.set_paths().

By default args to quiet mode and no traceback are passed. Usually this just runs automatic tests. If some of them fail, it’s further analyzed in some other tool in IDE.

mypythontools.tests.setup_tests(matplotlib_test_backend=False)[source]

Add paths to be able to import local version of library as well as other test files.

Value Mylogging.config.COLOR = 0 changed globally.

Note

Function expect tests folder on root. If not, test folder will not be added to sys path and imports from tests will not work.

Parameters

matplotlib_test_backend (bool, optional) – If using matlplotlib, it need to be closed to continue tests. Change backend to agg. Defaults to False.

mypythontools.tests.test_readme(readme_path=None, test_folder_path=None)[source]

Run python scripts from README.md

Parameters
  • readme_path ((str, pathlib.Path), optional) – If None, autodetected (README.md or readme.md on root). Defaults to None.

  • test_folder_path ((str, pathlib.Path), optional) – If None, autodetected (if root / tests). Defaults to None.

Raises
  • RuntimeError – If any test fails.

  • FileNotFoundError – If Readme not found.

Note

Only blocks with python defined syntax will be evaluated. Example:

`python import numpy `

First generate test file manually and control if test file generated as supposed with:

phmdoctest path_to_readme/README.md --outfile path_to_project/tests/test_readme_generated.py

If you want to import modules and use some global variables, add <!–phmdoctest-setup–> this directive above block with setup code. If you want to skip some test, add <!–phmdoctest-mark.skip–>