ch06_unittest.rst 3.36 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
9
Introduction to ``unittest``
----------------------------
BO ZHANG's avatar
tweaks    
BO ZHANG committed
10
Here we show an example unit test on ``csst_proto.top_level_interface.flip_image``.
BO ZHANG's avatar
BO ZHANG committed
11
12
13
14
15
16
17

.. literalinclude:: ../../tests/test_flip_image.py
   :linenos:
   :language: python



BO ZHANG's avatar
tweaks    
BO ZHANG committed
18
Run unit tests
BO ZHANG's avatar
BO ZHANG committed
19
20
--------------

BO ZHANG's avatar
BO ZHANG committed
21
To run unit tests
BO ZHANG's avatar
BO ZHANG committed
22

BO ZHANG's avatar
BO ZHANG committed
23
.. code-block:: bash
BO ZHANG's avatar
BO ZHANG committed
24

BO ZHANG's avatar
BO ZHANG committed
25
    pytest --import-mode=importlib
BO ZHANG's avatar
BO ZHANG committed
26

BO ZHANG's avatar
BO ZHANG committed
27
To run unit tests with coverage
BO ZHANG's avatar
BO ZHANG committed
28

BO ZHANG's avatar
BO ZHANG committed
29
.. code-block:: bash
BO ZHANG's avatar
BO ZHANG committed
30

BO ZHANG's avatar
BO ZHANG committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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
BO ZHANG's avatar
BO ZHANG committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112


Data access
-----------
For the Main Survey, i.e., the multi-band imaging (MBI) and the slit-less spectra (SLS)
developers might use two kinds of data to run their unit tests.

Auxiliary data (data used for your unit test ONLY)
   It should be put in ``/nfsdata/users/csstpipeline/L1Pipeline/unittests/<your_project_name>/``
   You can obtain this path from the environment in your unit test scirpts,

   .. code-block:: python

      import os
      os.getenv("AUX_DIR")


Simulation data (officially released simulation data)
   It should be accessed with the ``csst_common.data_manager.CsstMsDataManager``.
   The ``CsstMsDataManager.quickstart`` provides an interface to initialize
   an instance. See the next chapter for more details.

   Here we show an example which initializes the ``CsstMsDataManager`` with the 100th exposure of ``C5.2``
   simulation to perform a unit test for the project ``csst_ms_sls_instrument``.

   .. code-block:: python

        import os
        import unittest
        from csst_common.data_manager import CsstMsDataManager
        from csst_ms_sls_instrument.top_level_interface import sls_instrument_pipeline


        aux_dir = os.getenv("AUX_DIR")
        out_dir = os.path.join(aux_dir, "csst_ms_sls_instrument")


        class TestCsstMsSlsInstrument(unittest.TestCase):
            def setUp(self) -> None:
                self.dm = CsstMsDataManager.quickstart(ver_sim="C5.2", datatype="sls", dir_l1=out_dir, exposure_id=100)

            def test_instrument(self):
                test_detector = 1
                # for l0 image path:
                self.dm.l0_detector(test_detector)
                # for l1 image path:
                self.dm.l1_detector(detector=test_detector, post="flt.fits")
                self.dm.l1_file(name="merged.fits", comment="This is a merged image.")
                self.assertTrue(os.path.exists(self.dm.l0_detector(test_detector)))



For developers on other instruments (CPIC, THZ, MCI and IFS),
unit tests might be performed in their local environments.