Python/Testing: Difference between revisions
Brodriguez (talk | contribs) (Create initial page) |
Brodriguez (talk | contribs) (Add another pytest package reference) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ warn | For all testing, make sure each project directory has an {{ ic |__init__.py}} file in each Python directory, even if the file is empty. Otherwise tests likely will skip the directory.}} | |||
== Useful Testing Links == | == Useful Testing Links == | ||
Line 4: | Line 7: | ||
* [https://docs.python.org/3/library/unittest.html Python Docs - UnitTesting] | * [https://docs.python.org/3/library/unittest.html Python Docs - UnitTesting] | ||
* [https://docs.pytest.org/en/stable/contents.html Pytest Docs] | * [https://docs.pytest.org/en/stable/contents.html Pytest Docs] | ||
* [https://django-expanded-test-cases.readthedocs.io/en/latest/ Django Expanded Test Cases] | * [https://coverage.readthedocs.io/ Code Coverage Docs] | ||
* [https://django-expanded-test-cases.readthedocs.io/en/latest/ Django Expanded Test Cases Docs] | |||
== UnitTest Vs Pytest == | |||
[https://docs.python.org/3/library/unittest.html#command-line-interface UnitTest ] is the default testing framework provided with Python. | |||
== Useful | Meanwhile, [https://docs.pytest.org/en/stable/contents.html Pytest] is technically an external Python package. But in my experience, it tends to be a little bit more robust. | ||
== Using Pytest == | |||
To install Pytest, install the following base package to your desired python environment. Optionally add additional pytest packages for further functionality. | |||
pytest # Base Pytest package. | |||
pytest-asyncio # Additional Pytest logic for asyncio support. | |||
pytest-django # Additional Pytest logic for Django support. | |||
pytest-subtests # Additional Pytest logic for improved subtest support. | |||
pytest-xdist # Additional Pytest features, such as multithreading and looping. | |||
=== Basic Configuration File === | |||
For basic, minimal configuration, also create the following {{ ic |pytest.ini}} file at project root: | |||
{{ hc |pytest.ini|<nowiki> | |||
[pytest] | |||
python_files = tests.py test_*.py | |||
log_level = NOTSET | |||
</nowiki>}} | |||
To define additional environment settings on test run, you can add additional lines. For example for [[Programming/Django | Django]], you probably want to add: | |||
DJANGO_SETTINGS_MODULE = path.example.to.project.settings | |||
Where {{ ic |path.example.to.project.settings}} is replaced with the relative path to the desired Django settings file. | |||
=== Running Pytest === | |||
With the above configurations, you can then run Pytest with the following commands, or variations thereof: | |||
pytest # Run pytest on all files that match ini definition. | |||
pytest path/to/check # Run PyTest on a specific folder. | |||
pytest -n auto # Run PyTest parallel, if pytest-xdist is installed. | |||
== Useful Console Tweaks == | |||
To automatically clear console before running tests, edit your local {{ ic |bash.rc}} (or equivalent) file and add the following: | To automatically clear console before running tests, edit your local {{ ic |bash.rc}} (or equivalent) file and add the following: | ||
function pytest() { | function pytest() { | ||
clear | clear | ||
<optionally_include_extra_setup_functionality_here> | |||
pytest $@ | pytest $@ | ||
Line 22: | Line 70: | ||
== Checking Code Coverage == | == Checking Code Coverage == | ||
See [[ Python/Testing/CodeCoverage ]] |
Latest revision as of 03:27, 12 November 2022
__init__.py
file in each Python directory, even if the file is empty. Otherwise tests likely will skip the directory.
Useful Testing Links
UnitTest Vs Pytest
UnitTest is the default testing framework provided with Python.
Meanwhile, Pytest is technically an external Python package. But in my experience, it tends to be a little bit more robust.
Using Pytest
To install Pytest, install the following base package to your desired python environment. Optionally add additional pytest packages for further functionality.
pytest # Base Pytest package. pytest-asyncio # Additional Pytest logic for asyncio support. pytest-django # Additional Pytest logic for Django support. pytest-subtests # Additional Pytest logic for improved subtest support. pytest-xdist # Additional Pytest features, such as multithreading and looping.
Basic Configuration File
For basic, minimal configuration, also create the following pytest.ini
file at project root:
pytest.ini
[pytest] python_files = tests.py test_*.py log_level = NOTSET
To define additional environment settings on test run, you can add additional lines. For example for Django, you probably want to add:
DJANGO_SETTINGS_MODULE = path.example.to.project.settings
Where path.example.to.project.settings
is replaced with the relative path to the desired Django settings file.
Running Pytest
With the above configurations, you can then run Pytest with the following commands, or variations thereof:
pytest # Run pytest on all files that match ini definition. pytest path/to/check # Run PyTest on a specific folder. pytest -n auto # Run PyTest parallel, if pytest-xdist is installed.
Useful Console Tweaks
To automatically clear console before running tests, edit your local bash.rc
(or equivalent) file and add the following:
function pytest() { clear <optionally_include_extra_setup_functionality_here> pytest $@ }
This is very useful for removing clutter, so when tests fail, you only see results directly related to the recent failure.
Alternatively, change the function name if you wish to keep the base pytest
command, while still adding additional functionality.