The test suite uses the pytest
module.
When commit is pushed to a GitHub repository, the test suite is automatically run by GitHub Actions.
You can click the codecov badge in README to find out which part of the code is covered and which is not.
To run the test suite, one needs the pytest
module, that can be installed e.g. via pip install --user pytest
.
In the tests
folder, run pytest
.
You can run a specific test by giving its name as argument: pytest test_kubo.py
.
Run pytest -h
for further information about pytest
.
After running tests, many files are created.
These output files should be listed in the tests/.gitignore
file.
These ignored files can be deleted by git clean -Xd -f
.
Be careful because it will delete all ignored files, including those not related to tests.
Interactive cleaning can be used by git clean -Xd -i
.
- Fixtures
Fixtures are functions that arranges data for the tests. A function is marked as a fixture by the
@pytest.fixture
decorator. To use a fixture in a test or in another fixture, the name of the fixture should be included in the argument of the function where it is used.The results of the fixture is reused within its scope. The default scope is function. For example, the fixture of
system_Fe_W90
fixture issession
. So, theSystem
object is computed only once during the test.
- Test functions
Tests are written as functions. The name of each test function should start with
test_
. Variables can be tested using theassert
statement.
- Create/choose a system
A
System
object can be used from the list of fixtures. If you need to create a newSystem
object, you need to write a new fixture.- Creating a new ab initio data: If you add an additional dataset for the test computed using an external program (such as Quantum ESPRESSO), create a new folder in
tests/data
and include all the input and pseudopotential files to regenerate the data. Also, compress large text files such as the*.mmn
and*.amn
files. If possible, do not add large data files such as theuHu
,sHu
, andsIu
files to the repository; they can be created inside the system fixture using themmn2uHu
utility.
- Creating a new ab initio data: If you add an additional dataset for the test computed using an external program (such as Quantum ESPRESSO), create a new folder in
- Create/choose a parser
When comparing the
*.dat
files written from anEnergyResult
object, you can use thecompare_energyresult
fixture. When a different type of file should be parsed, write a new fixture for that type of file.
- Create a test
Tests that test a single module can be grouped into a single file. For example,
test_kubo.py
tests functionalities in the__kubo.py
module.To make the tests run faster, expensive calculations such as
wannierberri.integrate
should be done inside a fixture withscope="module"
. Then, the calculation results are reused for the functions inside the file. Test functions can request the results by have the fixture name in the argument.Note that by default,
pytest.approx
uses a relative tolerance of1e-6
. So,assert
may be problematic if the values are close to zero. You can use theabs
argument of thepytest.approx
function to set an absolute tolerance. If an absolute tolerance is set, the default relative tolerance is not used.