Python/Testing
__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-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.