unittest.rst 1.11 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
5
6
Unit Tests
==========
Unit tests should be based on `unittest`_,
while `doctest`_ is not recommended for limited usage.
In the end, `pytest`_ is used to perform the unittests.
And `coverage`_ is used to assess the unit test coverage.
BO ZHANG's avatar
BO ZHANG committed
7

BO ZHANG's avatar
BO ZHANG committed
8
To run unit tests
BO ZHANG's avatar
BO ZHANG committed
9

BO ZHANG's avatar
BO ZHANG committed
10
.. code-block:: bash
BO ZHANG's avatar
BO ZHANG committed
11

BO ZHANG's avatar
BO ZHANG committed
12
    pytest --import-mode=importlib
BO ZHANG's avatar
BO ZHANG committed
13

BO ZHANG's avatar
BO ZHANG committed
14
To run unit tests with coverage
BO ZHANG's avatar
BO ZHANG committed
15

BO ZHANG's avatar
BO ZHANG committed
16
.. code-block:: bash
BO ZHANG's avatar
BO ZHANG committed
17

BO ZHANG's avatar
BO ZHANG committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    coverage run -m pytest --import-mode=importlib

To display the coverage report

.. code-block:: bash

    coverage report

To generate the coverage report in html format

.. code-block:: bash

    coverage html

.. _unittest: https://docs.python.org/3.9/library/unittest.html
.. _doctest: https://docs.python.org/3.9/library/doctest.html
.. _pytest: https://docs.pytest.org/en/7.1.x
.. _coverage: https://coverage.readthedocs.io/en/6.5.0/


Conventions
-----------
Before write his own unit test scripts, a developer should have a glance at
the conventions on how `pytest`_ discovers unit tests (e.g, `test_*.py` or `*_test.py` files).

For more information, click `here`_.

.. _here: https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#test-discovery