mypythontools_cicd.tests package¶
Module with functions around testing. You can run tests including doctest with generating coverage, you can generate tests from readme or you can configure tests in conftest with single call.
- mypythontools_cicd.tests.default_test_config¶
Default values for tests config. If something changes here, it will change in all the repos. You can edit any values in pipeline. Intellisense and help tooltip should help.
- Type
- class mypythontools_cicd.tests.TestConfig(*args, **kwargs)[source]¶
Bases:
Config
Allow to setup tests.
- config_fields: ConfigFields¶
- property extra_args¶
List of args passed to pytest.
- Type
None | list
- Default
None
- property install_package¶
Install package from setup.py.
- Type
bool | Literal[“auto”]
- Default
“auto”
First it solves import problems in tests, second, it tests setup.py and pyproject.toml. It runs pip install –upgrade –force-reinstall .. If there is library already installed, it will be reinstalled. If auto it will test it if setup.py is available in current working directory.
- property prepare_test_venvs¶
Create venvs with defined versions.
- Type
None | list[str]
- Default
[“3.7”, “3.10”, “wsl-3.7”, “wsl-3.10”]
- property prepare_test_venvs_path¶
Prepare venvs in defined path.
- Type
str
- Default
“tests/venv”
- property run_tests¶
Define whether run tests or not.
- Type
bool
- Default
True
- property stop_on_first_error¶
Whether stop on first error.
- Type
bool
- Default
True
- property sync_test_requirements¶
Define whether update libraries versions.
- Type
None | Literal[“infer”] | PathLike | Sequence[PathLike]
- Default
[“requirements.txt”]
If using virtualenvs define what libraries will be installed by path to requirements. It can also be a list of more files e.g
["requirements.txt", "requirements_dev.txt"]
. If “infer”, auto detected (all requirements), not recursively, only on defined path.
- property sync_test_requirements_path¶
Define the root if using just names or relative path, and not found.
- Type
PathLike
- Default
PROJECT_PATHS.root
It’s also necessary when using another referenced files. If inferring files, it’s used to search. Defaults to PROJECT_PATHS.root.
- property test_coverage¶
Whether run test coverage plugin. If True, library pytest-cov must be installed.
- Type
bool
- Default
True
- property tested_path¶
Define path of tested folder.
- Type
None | PathLike
- Default
None
If None, root is used. Root is necessary if using doctest, ‘tests’ omits docstrings tests from code.
- property tests_path¶
If None, tests is used. It means where venv will be stored etc.
- Type
None | PathLike
- Default
None
- property verbosity¶
Define whether print details on errors or keep silent.
- Type
Literal[0, 1, 2]
- Default
1
If 0, no details, parameters -q and –tb=line are added. if 1, some details are added –tb=short. If 2, more details are printed (default –tb=auto).
- property virtualenvs¶
Virtualenvs used to testing. It’s used to be able to test more python versions at once.
Example
["tests/venvs/3.7", "tests/venvs/3.10"]
. If you want to use current venv, use [sys.prefix].- Type
None | Sequence[PathLike]
- Default
[“tests/venv/3.7”, “tests/venv/3.10”]
If no virtualenvs nor wsl_virtualenvs is configured, then python that called the function will be used.
- property wsl_virtualenvs¶
Define which wsl virtual environments will be tested via wsl.
- Type
None | Sequence[PathLike]
- Default
[“tests/venv/wsl-3.7”, “tests/venv/wsl-3.10”]
- mypythontools_cicd.tests.add_readme_tests(readme_path: None | PathLike = None, tests_folder_path: None | PathLike = None) None [source]¶
Generate pytest tests script file from README.md and save it to tests folder.
Can be called from conftest.
- Parameters
readme_path (None | PathLike, optional) – If None, autodetected (README.md, Readme.md or readme.md on root). Defaults to None.
tests_folder_path (None | PathLike, optional) – If None, autodetected (if root / tests). Defaults to None.
- Raises
FileNotFoundError – If Readme not found.
Example
>>> add_readme_tests()
Readme tests found.
Note
Only blocks with python defined syntax will be evaluated. Example:
```python import numpy ```
If you want to import modules and use some global variables, add
<!--phmdoctest-setup-->
directive before block with setup code. If you want to skip some test, add<!--phmdoctest-mark.skip-->
- mypythontools_cicd.tests.deactivate_test_settings() None [source]¶
Deactivate functionality from setup_tests.
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_cicd.tests.run_tests(config: ~mypythontools_cicd.tests.tests_internal.TestConfig = <mypythontools_cicd.tests.tests_internal.TestConfig object>) None [source]¶
Run tests. If any test fails, raise an error.
This is not supposed for normal testing during development. It’s usually part of pipeline an runs just before pushing code. It usually runs on more python versions and it’s syncing dependencies, so takes much more time than testing in IDE.
- Parameters
config (PipelineConfig, optional) – TestConfig configuration object. Just import default_test_config and use intellisense and help tooltip with description. Defaults to default_test_config.
- Raises
Exception – If any test fail, it will raise exception (git hook do not continue…).
Note
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.
Example
run_tests(verbosity=2)
- mypythontools_cicd.tests.setup_tests(generate_readme_tests: bool = True, matplotlib_test_backend: bool = False, set_numpy_random_seed: int | None = 2) None [source]¶
Add paths to be able to import local version of library as well as other test files.
Value Mylogging.config.colorize = 0 changed globally.
There are some doctest directives added. E.g. this will test only on defined python version or newer:
tested_doctest_line # doctest: +3.9
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
generate_readme_tests (bool, optional) – If True, generate new tests from readme if there are new changes. Defaults to True.
matplotlib_test_backend (bool, optional) – If using matlplotlib, it need to be closed to continue tests. Change backend to agg. Defaults to False.
set_numpy_random_seed (int | None) – If using numpy random numbers, it will be each time the same. Numpy is not in requirements, so it need to be installed. It’s skipped if not available. Defaults to 2.