Python
FreeBSD
Install following packages
- py39-sqlite3 - otherwise sqlite3 doesn’t work even in virtual environment
- py39-flake8
- pylint-py39
- py39-pytest - Needed by pylint to correctly handle imports
Virtual environments
Create virtual environment (this will use default python).
python3 -m venv 'envname'Start virtual environment
source 'envname'/bin/activateUpgrade pip in virtual environment
python3 -m pip install --upgrade pipTesting
pytest
- Allows to run standalone test function as its own case, unlike unittest, where you need to put your test into classes and handle class inheritance. This is great because that means it’ll be easier for you to define tests and also easier for the next programmer to come along and maintain your tests.
- Easy to read syntax, allowing you to use standard assert method in combination with comparison operators to test scenarios in your test functions. Unittest, on the other hand, has over 10 different assertion methods for tests. While they are specific, they have changed in different versions of Python. This requires you to keep up with these changes and update your project with the proper method if the old version becomes deprecated.
- Powerful command line interface to control the tests you’d like to run over or even skip with markers. This comes in handy when you are updating a portion of your test and don’t want to run the entire test suite as you iterate through a small set of changes.
- Furthermore, pytest uses fixtures to automate test setup, teardown, and common test scenarios for use in varying scopes. Fixtures also help you share data and code between tests that need common setup functions. The test automation Python provides doesn’t stop there. It also comes with a feature called parameterization, which enables you to automate test functions to run over a variety of scenarios in one swoop. This makes pytest a great framework for complex testing scenarios, unlike doctest, which is great for basic tests but it limits your test to doc strings.
python3 -m pip install -r test-requirements.txtWhere test-requirements.txt list required python
packages:
coverage
pytest
pytest-cov
pytest-flakes
pytest-pep8
pytest-pythonpath
Run the tests
pytestResources
- Course Unit Testing in Python
- Book Python Testing with pytest
- Getting Started With Testing in Python