Create simple unit testing sample and understand good naming and generation of samples ...
The inspiration for this is taken from this article https://testdriven.io/blog/modern-tdd/
-
Install
pip install -U pytest
-
pytest is the test framework used by the article
-
Documentation can be found https://docs.pytest.org/en/stable/getting-started.html
-
Running the tests through python I setup
python3 -m pytest
. NOTE: the test name always needs to start with test_ or end with _test or the test discovery will not happen -
VSCode Tips and tricks:
-
With vscode, I needed to setup my interpreter path to utilise Python 3.9.1 for everythign to work with the correct version of Python. This can be found on the bottom left panel of your IDE footer
-
Setting up text explorer, add the extension python test explorer and then see this https://code.visualstudio.com/docs/python/testing for more information
-
Configure settings with Pytest
{ "python.pythonPath": "/opt/local/bin/python3", "python.testing.pytestArgs": [ "tests" ], "python.testing.unittestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.pytestEnabled": true, "python.testing.autoTestDiscoverOnSaveEnabled": true }
-
-
Install pip with python 3 or upgrade pip to the latest
python3 -m pip install python3 -m pip install --upgrade pip
-
Create a virtual environment using
python3 -m venv ./opt/local
-
Setting up a project structure should resemble https://docs.python-guide.org/writing/structure/
-
Make sure if you have folder that you always include
__init__.py
files which can include paths, setting variables or just logging - see more here -
Pytest install all things needed:
pip install pytest pip install pytest-sugar pip install pytest-cov
pytest --fixtures
gives a list of fixtures, including one built up with setup fixtures that may have doc commentspytest --markers
gives a list of markers to help decorate functions withpip install pytest-html
and output html to show reports and output an html reportpytest --html=report.html
- Mocking details can be here within https://docs.python.org/3/library/unittest.mock.html#module-unittest.mock
pytest --cov-report html:cov_html --cov-branch --cov=<add specific module> .
for getting coverage of a specific module being testedpytest --cov=myproj tests/
should do a report for everything but it doesnt work for me at the moment