-
Notifications
You must be signed in to change notification settings - Fork 652
Style Guide: Tests
- We use
pytest
along withnumpy
(specifically thenumpy.testing
module) to write test cases for MDAnalysis. -
Do not use anything from
numpy.testing
that depends onnose
such asassert_raises
. - The testsuite follows the same directory structure as the package.
This style guide is meant to be concise and allow developers to quickly learn which constructs to use and which ones to avoid. The rationale for these choices is discussed in issue #1444.
-
Use plain
assert
statements for comparing single values. E.g., to check the length of an iterablefoo
, doassert len(foo) == expected_length
-
To check equality for a single value up to a certain precision, use
assert_almost_equals
fromnumpy.testing
. Do not manually round off the value and use plain assert statements. Do not usepytest.approx
-
To compare an iterable, use
assert_equal
fromnumpy.testing
. Do not iterate over and compare every single value. -
To compare an iterable up to a certain precision, use
assert_almost_equals
fromnumpy.testing
. -
Do not use
assert_array_equal
orassert_array_almost_equal
fromnumpy.testing
to compare array/array-like data structures. Instead, useassert_equal
orassert_almost_equal
.
-
Use classes to group tests if it makes sense (e.g., if the test class will be inherited by another test class and the code can be reused). We prefer subclassing over parametrizing classes (for examples, have a look at the topology module).
-
For everything else, use plain functions.
- To check for a particular exception, use the
pytest.raises(ExceptionName)
context manager. -
Do not use
assert_raises
fromnumpy.testing
. -
Do not use the
pytest.mark.raises
decorator.
- To check for a particular warning, use the
pytest.warns(WarningName)
context manager.
- To mark an expected failure, use
pytest.mark.xfail
decorator - To manually fail a test, make a call to
pytest.fail
Use pytest's inbuilt tmpdir
fixture
- To create a temp file
temporary_file = str(tmpdir.join('test.pdb'))
- To use a temp directory as the working directory, use
tmpdir.as_cwd
context manager
- To skip tests based on a condition, use
pytest.mark.skipif(condition)
decorator. - To skip a test if a module is not available for importing, use
pytest.importorskip('module_name')
- Use the
pytest.mark.parametrize
decorator to test the same function for different inputs
- Use fixtures as much as possible to reuse "resources" between test methods/functions (example: universe fixture). The rule of thumb is to use the largest possible scope for the fixture.