Code Testing
BERA Tools employs a pytest framework to ensure code quality and reliability. This document outlines the testing strategies, tools, and workflows used in the repository.
Testing Workflows
- pytest: Tests use pytest. The frame's tests are in
tests/, and each module's tests are inberatools/modules/<module>/tests/. The automated workflows described below run onlyberatools/modules/mapping/tests/test_workflow.py, not the complete test suite. - Automatic integration testing: The
python-integration-tests.ymlworkflow runsberatools/modules/mapping/tests/test_workflow.py, thenberatools/modules/pc2products/tests/test_r_environment.pyandberatools/modules/pc2products/tests/test_r_workflow.pyfor the PC2Products tools, on pushes tomainand on qualifying pull requests targetingmainwhen its configured path filters match. - Coverage: Coverage from
beratools/modules/mapping/tests/test_workflow.pyis printed in the GitHub Actions job log; it is not uploaded to an external service. - Runtime environment: The automatic integration workflow uses the Pixi-managed Python 3.12/GDAL environment.
- Manual compatibility testing: The dispatch-only
python-compatibility-tests.ymlworkflow runsberatools/modules/mapping/tests/test_workflow.pythrough tox in Python 3.12, 3.13, and 3.14 micromamba environments with conda-forge GDAL.
Running Tests Locally
To run tests locally, follow these steps:
- Install the required dependencies:
pip install .[dev]
- Run all the test suite using
pytest:
Discover and execute all tests in tests/ and beratools/modules/*/tests/.
pytest
Run a specific test file:
pytest beratools/modules/mapping/tests/test_tools.py
Run a specific test function within a file:
pytest beratools/modules/mapping/tests/test_tools.py::test_function_name
PC2Products (R) tests
The PC2Products tools need R (see Installation). Their sample tiles are in
beratools/modules/pc2products/tests/data/LiDAR/; the tools write rasters to
beratools/modules/pc2products/tests/data/LiDAR/Raster_Output/ and point clouds to
beratools/modules/pc2products/tests/data/LiDAR/Classified_PC/ and beratools/modules/pc2products/tests/data/LiDAR/Normalized_PC/, all git-ignored.
| Tests | What they cover | Needs R |
|---|---|---|
beratools/modules/pc2products/tests/test_r_tools.py |
Runs each LiDAR tool on the sample tiles and checks the output | Yes |
beratools/modules/pc2products/tests/test_r_workflow.py |
Runs CSF classification, DTM, DSM, normalization, CHM (from the normalized tiles) and hummock/hollow in sequence, checking each tool's output, then deletes the outputs | Yes |
beratools/modules/pc2products/tests/test_r_environment.py |
Installs any missing R packages the tools need, then checks that they load | Yes |
beratools/modules/pc2products/tests/test_surface_water.py, beratools/modules/pc2products/tests/test_dtw.py, beratools/modules/pc2products/tests/test_seismic_line_analysis.py |
Run the raster tools on synthetic inputs with a known answer, built by beratools/modules/pc2products/tests/synthetic_data.py |
Yes |
tests/pytest_qt/test_r_tool_forms.py |
GUI forms for each PC2Products tool, and GUI-to-validation round trips | No |
python -m pytest beratools/modules/pc2products/tests/test_r_tools.py beratools/modules/pc2products/tests/test_r_environment.py -v
The first run of the R tests installs the R packages into the environment's R, so it can take a while.
Windows: run the R tests from PowerShell or cmd
Run the R tests from PowerShell or cmd, not Git Bash. Under Git Bash, rpy2's
R CMD config call picks up Git's sh, fails, and rpy2 can't start R.
GitHub Actions
BERA Tools uses GitHub Actions to automate testing and deployment processes. This document describes the various workflows set up in the repository to ensure code quality and streamline releases.
-
python-compatibility-tests.yml: This dispatch-only workflow runs
beratools/modules/mapping/tests/test_workflow.pythrough tox under Python 3.12, 3.13, and 3.14 in micromamba environments with conda-forge GDAL and native geospatial libraries. -
python-integration-tests.yml: This automatic workflow runs
beratools/modules/mapping/tests/test_workflow.pywith terminal coverage on pushes tomainand qualifying pull requests targetingmain. It then installs the system GDAL/GEOS/PROJ/udunits libraries and runsberatools/modules/pc2products/tests/test_r_environment.py, which installs the R packages into the pixi R, andberatools/modules/pc2products/tests/test_r_workflow.py. The job's timeout is 60 minutes to leave room for the R packages.
Refer to the Maintainer Guide for more information on these workflows.
Write Tests
It is required to write tests when contributing to BERA Tools. follow these guidelines to ensure consistency and quality in the test suite.
conftest.py
tests/conftest.py holds the fixtures and configuration of the frame's tests, and each module's tests/conftest.py holds its own, so a module's tests also run on their own. Put setup code that several test files share in the conftest.py next to them.
Test Organization
The frame's tests (core, GUI, CLI and registry) are in tests/. A module's tests are in beratools/modules/<module>/tests/, with their sample data in its tests/data/. Tests that use tools of more than one module go in tests/test_cross_module.py. Each tool should have a corresponding test file.
The module test folders have no __init__.py, so give a helper module a name that no other test folder uses, such as mapping_test_utils.py.
Naming Conventions
Test files should be named with the prefix test_ which can be detected by pytest automatically. The test file name should be followed by the module or tool name (e.g., test_tools.py).
Writing Tests
When writing tests, use assertions to verify that the code behaves as expected. Utilize fixtures from the conftest.py files for setup and cleanup operations.