Writing and Running Unit Tests¶
CCL uses the
pytest package to run a suite of unit tests. This package can
be installed via
[pip|conda] install pytest.
Running the Unit Tests¶
To run the unit tests, execute
pytest -vv pyccl
from the top-level repository directory. Any errors will be reported at the end.
pytest options include
-k <name of test>: adding this option with the name of the test will force
pytestto only run that test
-s: this option makes
pytestprint all output to STDOUT instead of its usual behavior of suppressing output
-x: this option forces
pytestto stop after the first failed test
--pdb: this option will launch the
Pythondebugger for failed tests, usually you want to combine it with
-s -x -k <name of test>in order to debug a single failed test
Writing Unit Tests¶
Please follow the following guidelines when writing unit tests.
- All unit tests should be written as modules in the
- Each unit test is a function that does some operation with CCL and then
assertstatement. A unit test is marked by
pytestas failed when the
- The unit tests should be fast, ideally less than a second or so. This requirement
means avoiding running
CAMBwhen it is not needed.
- The unit tests file should have a name that matches the python module it is
- The unit test function itself should have a name that starts with
test_and is also descriptive.
An example of a unit test would be a file at
import numpy as np import pyccl def test_cosmology_sigma8_consistent(): cosmo = pyccl.Cosmology( Omega_c=0.25, Omega_b=0.05, h=0.7, sigma8=0.8, n_s=0.95, transfer_function='bbks') assert np.allclose(pyccl.sigma8(cosmo), 0.8)
Building and Running CCL’s C Unit Tests¶
CCL has a few vestigial unit tests written in
C. Once you have
installed (see Getting CMake), you can build them with the following
commands from the top-level CCL directory
$ mkdir -p build $ cd build $ cmake .. $ make check_ccl
Then you can run the tests via