Functional testing
Each function is compared to specifications to ascertain that it returns the required outputs.
Framework
For Functional testing, Pytest is preferred.
Setting up
Before installing pytest
, ensure you have python installed.
Manage your environment using conda
, venv
, pyenv
, poetry
or shell.nix
(NixOS).
Installation
First, we start by creating an environment(conda
specific.)
To install pytest
, open your terminal:
For an existing project with a requirements.txt
or environment.yml
file
# requirements.txt
conda create --name <env_name> --file requirements.txt
# or
# environment.yml
conda env create -f environment.yml
Usage
To use pytest
you need to create a tests
directory in your project.
Inside the test directory, you can create tests.
In the test file, import the pytest module.
Creating tests
The test files have a naming convention.
The file name has to have prefix test_
or suffix _test.py
.
For example: test_*.py
and *_test.py
respectively.
NOTE: Using the prefix test_
is preferred.
The test functions also do have a naming convention.
The function has to have a prefix test_.
For example:
To create new functional tests using pytest:
Function in Project/example.py
module.
Test function in tests/
directory.
import pytest
import Project.example
# test function
def test_addition():
c = addition(3, 2)
assert c == 5
...
Running tests
To run the tests, open your terminal and navigate to your test directory:
The above command will run all tests present in the tests/
directory.
To run a single test use:
For more, refer to invoking pytest.
Pytest fixtures
Fixtures are functions that provide resources or perform actions required for test execution. By encapsulating these setup and teardown steps into fixtures: - It streamlines the test code and promotes code re-usability - It improves the overall readability and maintainability of the test suite.
Fixture example that contains raster path that can be used by multiple tests:
import pytest
# fixtures
@pytest.fixture()
def vector_data():
path = 'vector.geojson'
return path
@pytest.fixture()
def raster_data():
path = 'raster.tif'
return path
def test_mask_raster(raster_data, vector_path):
raster = mask_raster(raster_data, vector_path)
...
Pytest in Django projects
pytest-django
is a pytest plugin used to test django applications.
Installation
To install pytest-django
, open you terminal and activate your environment.
Then type:
Setting up and configuration
pytest-django
is set up in the django_project
directory.
Navigate to the django_project
, ensure there is a pytest.ini
with the following configurations:
In the django_project
, there are django apps each in a different directory.
You need to create a tests
folder in each directory and add an __init.py__
file to make a package.
In the tests
directory, create tests based on guidelines.
An example of the directory structure:
$ project # root directory
django_project # django project
__init__.py
core
-> <django_app_1> # django application
__init__.py
-> tests # tests directory
__init__.py
-> test_example.py # test file <test_*.py>
frontend
manage.py
pytest.ini
Running tests
To run pytest
in django, navigate to the django_project
directory the run:
To run a particular test use:
Pytest with BDD
BDD library for pytest.