Directory Structure
|- test\
|- options.py
|- unit\
|- runner.py
|- test_contexts.py
|- ...
|- unit-distributed\
|- runner.py
|- test_contexts_arbmpi.py
|- test_contexts_mpi4py.py
|- ...
In parent folder test
:
-
options.py
: set global options (define arg parser)
In subfolders unit
/unit_distributed
:
-
test_xxxs.py
: define unittest class with test methods and own test suite (named: test module) -
runner.py
: run all tests in this subfolder (which are defined as suite in test modules)
Usage
unittest
from SUBFOLDER:
with - to run all tests in subfolder:
python -m unittest [-v]
- to run module:
python -m unittest module [-v]
, e.g. in test/unit
use python -m unittest test_contexts -v
- to run class in module:
python -m unittest module.class [-v]
, eg. in test/unit
use python -m unittest test_contexts.Contexts -v
- to run method in class in module:
python -m unittest module.class.method [-v]
, eg. in test/unit
use python -m unittest test_contexts.Contexts.test_context -v
runner.py
and argument(s) -v {0,1,2}
from SUBFOLDER:
with - to run all tests in subfolder:
python -m runner[-v2]
or python runner.py [-v2]
- to run module:
python -m test_xxxs [-v2]
or python test_xxxs.py [-v2]
- running classes or methods not possible this way
from any other folder:
- to run all tests:
python path/to/runner.py [-v2]
- to run module:
python path/to/test_xxxs.py [-v2]
Adding new tests
- In suitable folder
test/unit
(no MPI) ortest/unit_distributed
(MPI), createtest_xxxs.py
file - In
test_xxxs.py
file, define a) a unittestclass Xxxs(unittest.TestCase)
with test methodstest_yyy
b) a suite functionsuite()
consisting of all desired tests returning a unittest suiteunittest.makeSuite(Xxxs, ('test'))
(for all defined tests, tuple of selected tests possible); steering of which tests to include happens here! c) a run functionrun()
with a unittest runnerunittest.TextTestRunner
running thesuite()
viarunner.run(suite())
d) aif __name__ == "__main__":
callingrun()
- Add module to
runner.py
in subfolder by addingtest_xxxs
a) to import: intry
addimport test_xxxs
, inexcept
addfrom test.subfolder import test_xxxs
b) totest_modules
list
Naming convention
- modules:
test_xxxs
(all lower case, ending withs
since module can consist of multiple classes) - class(es):
Xxxs
(first letter upper case, ending withs
since class can consist of multiple test functions) - functions:
test_yyy
(always starting withtest
since suite is build from all methods starting withtest
)