You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/tests/pytest_framework.rst
+79-27Lines changed: 79 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,9 @@ simulation is run with two versions of ``enzo-e`` and their results are compared
9
9
This is useful for testing problems with no analytical solution or generally
10
10
verifying that results from commonly run simulations don't drift.
11
11
12
+
It is also useful in for testing problems that do have analytic solutions (the answer test might quantify how close a simulation result is to the analytic expected solution).
13
+
While such tests do exist in the ctest-framework, they often involve more boiler-plate code.
14
+
12
15
`pytest <https://docs.pytest.org/>`__ is a Python-based framework for detecting
13
16
and running a series of tests within a source code repository. When running
14
17
``pytest``, the user can provide a directory in which ``pytest`` will look for
@@ -41,6 +44,8 @@ other useful answer testing functionality are located in the source in
41
44
`test/answer_tests/answer_testing.py`. All answer tests are located in the
42
45
other files within the `test/answer_tests` directory.
43
46
47
+
Some other functionality, that may be reused in other unrelated scripts provided in the Enzo-E repository, are provided in the ``test_utils`` subdirectory.
48
+
44
49
Running the Answer Test Suite
45
50
-----------------------------
46
51
@@ -60,16 +65,49 @@ To generate test answers, use the highest numbered gold standard tag.
60
65
Configuring the Answer Test Suite
61
66
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62
67
63
-
Before the answer tests can be run, a few environment variables must be set to
64
-
configure behavior.
68
+
The behavior of the test can be configured by passing command line arguments to ``pytest`` or by setting environment variables (or by mixing both).
69
+
70
+
When invoking ``pytest``, the command line flags discussed here should be passed **after** the path to the `test/answer_tests` directory has been provided.
71
+
For the sake of example (the meaning of flags are explained below), one might invoke:
72
+
73
+
.. code-block:: bash
65
74
66
-
* ``TEST_RESULTS_DIR``: points to a directory in which answers will be stored
67
-
* ``CHARM_PATH``: points to the directory in which ``charmrun`` is located
68
-
* ``ENZO_PATH``: points to the ``enzo-e`` binary to use.
69
-
If this is not specified, this defaults to the ``<PATH/TO/ENZO-E/REPO>/build/bin/enzo-e``
70
-
* ``GENERATE_TEST_RESULTS``: "true" to generate test results, "false" to compare with existing results.
71
-
* ``GRACKLE_INPUT_DATA_DIR``: points to the directory where ``Grackle`` input files are installed.
72
-
If not specified, then all tests involving ``Grackle`` will be skipped.
75
+
$ pytest test/answer_tests \
76
+
--build-dir ./build \
77
+
--answer-store
78
+
79
+
The following table lists command line flags, and where applicable, the environment variables that they are interchangable with.
80
+
In cases where both are set, the command line argument is given precedence.
81
+
82
+
.. list-table:: Configuring pytest behavior
83
+
:widths: 10 10 30
84
+
:header-rows: 1
85
+
86
+
* - flag
87
+
- env var
88
+
- description
89
+
* - ``--build-dir``
90
+
- N/A
91
+
- points to the build-directory where the target enzo-e binary was built (that binary has the path: BUILD_DIR/bin/enzo-e).
92
+
The path to the charmrun launcher will be inferred from the `BUILD_DIR/CMakeCache.txt` file, but can be overwritten by the ``--charm`` flag or the ``CHARM_PATH`` environment variable.
93
+
This precedence was chosen in case a user causes a change to relevant cached build-variables, but have not rebuilt Enzo-E (i.e. `CMakeCache.txt` may not be valid for the binary).
94
+
When this flag isn't specified, the test infrastructure searches for the enzo-e binary at ENZOE_ROOT/build/bin/enzo-e, but doesn't try to infer charmrun's location from `CMakeCache.txt`.
95
+
* - ``--local-dir``
96
+
- ``TEST_RESULTS_DIR``
97
+
- points to a directory in which answers will be stored/loaded
98
+
* - ``--charm``
99
+
- ``CHARM_PATH``
100
+
- points to the directory in which ``charmrun`` is located
101
+
* - ``--answer-store``
102
+
- ``GENERATE_TEST_RESULTS``
103
+
- When the command line flag is specified, test results are generated. Otherwise, results are compared against existing results (unless the environment variable is specified).
104
+
The environment variable can be be set to ``"true"`` to generate test results or ``"false"`` to compare with existing results.
105
+
* - ``--grackle-input-data-dir``
106
+
- ``GRACKLE_INPUT_DATA_DIR``
107
+
- points to the directory where ``Grackle`` input files are installed.
108
+
If not specified, then all tests involving ``Grackle`` will be skipped.
109
+
110
+
Earlier versions of the tests also required the ``"USE_DOUBLE"`` environment variable to be set to ``"true"`` or ``"false"`` to indicate whether the code had been compiled in double or single precision.
73
111
74
112
.. code-block:: bash
75
113
@@ -83,21 +121,16 @@ First, check out the highest numbered gold standard tag and compile ``enzo-e``.
83
121
84
122
.. code-block:: bash
85
123
86
-
$ git checkout gold-standard-1
124
+
# in the future, you will need to subsitute 004 for a higher number
125
+
$ git checkout gold-standard-004
87
126
$ ...compile enzo-e
88
127
89
-
Then, configure the test suite to generate answers by setting
90
-
GENERATE_TEST_RESULTS to true.
91
-
92
-
.. code-block:: bash
93
-
94
-
$ export GENERATE_TEST_RESULTS=true
95
-
96
-
Finally, run the test suite by calling ``pytest`` with the answer test directory.
128
+
Then, run the test suite by calling ``pytest`` with the answer test directory (make sure to configure behavior correctly with command-line arguments or environment variables).
129
+
In the following snippet, we assume you are currently at the root of the Enzo-E repository and that you will replace ``<build-dir>`` with the directory where you build enzo-e (this is commonly ``./build``)
========================== test session starts ===========================
102
135
platform linux -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
103
136
rootdir: /home/circleci/enzo-e
@@ -116,19 +149,16 @@ Comparing Test Answers
116
149
117
150
Once test answers have been generated, the above steps need not be repeated until
118
151
the gold standard tag has been updated. Now, any later version of the code can be
119
-
run with the test suite to check for problems. Set the GENERATE_TEST_RESULTS
120
-
environment variable to false to configure the test suite to compare with existing
121
-
answers.
152
+
run with the test suite to check for problems. To configure the test suite to compare with existing answers, omit the ``--answer-store`` flag and ensure that the ``GENERATE_TEST_RESULTS`` variable is either unset or set to ``"false"``.
By default, most output printed by ``enzo-e`` or the test scripts will be swallowed
134
164
by ``pytest``. When tests fail, the Python traceback may be shown, but not much
@@ -139,7 +169,18 @@ variables when this flag is given.
139
169
140
170
.. code-block:: bash
141
171
142
-
$ pytest -s test/answer_tests
172
+
$ pytest -s test/answer_tests # other args...
173
+
174
+
When debugging an issue it's sometimes helpful to force pytest to run a subset of tests.
175
+
This can be accomplished with the ``-k`` flag.
176
+
For example, to only run a subset of tests with ``"grackle"`` in the test name, one might execute
177
+
178
+
.. code-block:: bash
179
+
180
+
$ pytest test/answer_tests -k "grackle"# other args...
181
+
182
+
When investigating a failing test or prototyping a brand-new test, it can sometimes be helpful to run the tests against multiple versions of enzo-e.
183
+
Rather than rebuilding Enzo-E each time you want to do that, you can instead build the different versions of Enzo-E in separate build-directories, and direct ``pytest`` to use the different builds with the ``--build-dir`` flag.
143
184
144
185
Creating New Answer Tests
145
186
-------------------------
@@ -291,6 +332,17 @@ in single or double precision, and adjust the tolerance on the tests accordingly
291
332
deftest_hllc_cloud(self):
292
333
...
293
334
335
+
.. note::
336
+
337
+
The above code is primarily for the sake of example.
338
+
In practice, we now automatically detect the code's precision from the enzo-e executable.
339
+
340
+
Alternatively, additional configuration options can be configured through new command-line flags, which are introduced and parsed by the `conftest.py` file in the `answer_test` directory.
341
+
This is generally more robust than adding environment variables (since the flags are more easily discovered and are more explicit).
342
+
But, in practice it's made slightly more complicated by the fact that flags are parsed with pytest hooks.
343
+
Flags added in this way work best with ``pytest`` fixtures, while our tests mostly leverage features from `Python's unittest module <https://docs.python.org/3/library/unittest.html>`_.
0 commit comments