44Tutorial
55********
66
7- Once :ref: `integrated in your project <integration >`, the ``Pytest::Pytest ``
7+ Once :ref: `integrated into your project <integration >`, the ``Pytest::Pytest ``
88target and the :func: `pytest_discover_tests ` function are available for using.
99
1010.. _tutorial/target :
1111
1212Using the target
1313================
1414
15- Let's consider a project which wraps C++ logic with Python bindings. We need to
16- add a :file: `CMakeLists.txt ` configuration file to add Python tests within the
17- same directory. The :term: `Pytest ` command can be easily implemented using the
18- :term: `add_test ` function:
15+ Let's consider a project that wraps C++ logic with Python bindings. We need to
16+ add a :file: `CMakeLists.txt ` configuration file to include Python tests within
17+ the same directory. The :term: `Pytest ` command can easily be implemented using
18+ the :term: `add_test ` function:
1919
2020.. code-block :: cmake
2121
@@ -25,7 +25,7 @@ same directory. The :term:`Pytest` command can be easily implemented using the
2525 )
2626
2727 For the tests to run, the :envvar: `PYTHONPATH ` environment variable must be
28- updated to locate the built package library. We can use an expression generator
28+ updated to locate the built package library. We can use a generator expression
2929to resolve the path of the dependent target directory dynamically:
3030
3131.. code-block :: cmake
@@ -53,7 +53,7 @@ location should be provided:
5353 platform. :envvar: `LD_LIBRARY_PATH ` is used on Linux,
5454 :envvar: `DYLD_LIBRARY_PATH ` on macOS, and :envvar: `PATH ` on Windows.
5555
56- After building the project, the command can then be executed by :term: `CTest `.
56+ After building the project, the tests can then be executed using :term: `CTest `.
5757If all tests are successful, the output will look as follows:
5858
5959.. code-block :: console
@@ -74,7 +74,7 @@ as failed.
7474Using the function
7575==================
7676
77- A :func: `pytest_discover_tests ` function is provided to create :term: `CTest `
77+ A :func: `pytest_discover_tests ` function is provided to create :term: `CMake `
7878tests for each Python test collected. Therefore, the configuration added in the
7979previous section could be replaced by the following:
8080
@@ -86,65 +86,118 @@ previous section could be replaced by the following:
8686 $<TARGET_FILE_DIR:MyLibrary>
8787 PYTHON_PATH_PREPEND
8888 $<TARGET_FILE_DIR:MyLibrary>
89- TRIM_FROM_NAME "^test_"
9089 DEPENDS MyLibrary
9190 )
9291
9392 This will create a new **PythonTest ** target, dependent on the **MyLibrary **
9493target.
9594
9695The expected environment can be defined simply with the ``LIBRARY_PATH_PREPEND ``
97- and ``PYTHON_PATH_PREPEND `` arguments , which both accept multiple values. The
96+ and ``PYTHON_PATH_PREPEND `` options , which both accept multiple values. The
9897environment variable used to locate shared libraries will be automatically
9998chosen according to the platform.
10099
101- Pytest usually requires tests to start with a
100+ A list of dependent targets can be defined with the ``DEPENDS `` option, which
101+ accepts multiple values.
102+
103+ After building the project, running :term: `CTest ` will display the tests as
104+ follows:
105+
106+ .. code-block :: console
107+
108+ Start 1: PythonTest.test_greet_world
109+ 1/4 Test #1: PythonTest.test_greet_world ........... Passed 0.47 sec
110+ Start 2: PythonTest.test_greet_john
111+ 2/4 Test #2: PythonTest.test_greet_john ............ Passed 0.47 sec
112+ Start 3: PythonTest.test_greet_julia
113+ 3/4 Test #3: PythonTest.test_greet_julia ........... Passed 0.47 sec
114+ Start 4: PythonTest.test_greet_michael
115+ 4/4 Test #4: PythonTest.test_greet_michael ......... Passed 0.54 sec
116+
117+ A fully identified test collected by :term: `Pytest ` might look like this:
118+
119+ .. code-block :: console
120+
121+ tests/test_module.py::TestMyClass::test_example
122+
123+ By default, only the class and function name of each :term: `Pytest ` test collected
124+ are used to create the :term: `CMake ` tests. You can use the ``INCLUDE_FILE_PATH ``
125+ option to include the file path within the name:
126+
127+ .. code-block :: cmake
128+ :emphasize-lines: 7
129+
130+ pytest_discover_tests(
131+ PythonTest
132+ LIBRARY_PATH_PREPEND
133+ $<TARGET_FILE_DIR:MyLibrary>
134+ PYTHON_PATH_PREPEND
135+ $<TARGET_FILE_DIR:MyLibrary>
136+ INCLUDE_FILE_PATH
137+ DEPENDS MyLibrary
138+ )
139+
140+ Pytest usually requires the test class and function to start with a
102141`specific prefix
103142<https://docs.pytest.org/en/latest/explanation/goodpractices.html> `_,
104- which can be trimmed using the ``TRIM_FROM_NAME `` argument. The value can use a
105- `regular expression < https://en.wikipedia.org/wiki/Regular_expression >`_ to
106- match the part of the test name that should be trimmed.
143+ which can be trimmed using the ``TRIM_FROM_NAME `` or `` TRIM_FROM_FULL_NAME ``
144+ options. The value can use a :term: `regular expression ` to match the part of
145+ the test name that should be trimmed.
107146
108- A list of dependent targets can be defined with the ``DEPENDS `` argument, which
109- accepts multiple values.
147+ The ``TRIM_FROM_FULL_NAME `` option can be used to trim parts of the entire name,
148+ while the ``TRIM_FROM_NAME `` option will be applied to the class, method and
149+ function name of each :term: `Pytest ` test collected for convenience.
110150
111- After building the project, running :term: `CTest ` will display the tests as
151+ .. code-block :: cmake
152+ :emphasize-lines: 7
153+
154+ pytest_discover_tests(
155+ PythonTest
156+ LIBRARY_PATH_PREPEND
157+ $<TARGET_FILE_DIR:MyLibrary>
158+ PYTHON_PATH_PREPEND
159+ $<TARGET_FILE_DIR:MyLibrary>
160+ TRIM_FROM_NAME "^(Test|test_)"
161+ INCLUDE_FILE_PATH
162+ DEPENDS MyLibrary
163+ )
164+
165+ After rebuilding the project, running :term: `CTest ` will display the tests as
112166follows:
113167
114168.. code-block :: console
115169
116170 Start 1: PythonTest.greet_world
117- 1/4 Test #1: PythonTest.greet_world ........... Passed 0.47 sec
171+ 1/4 Test #1: PythonTest.greet_world ............... Passed 0.47 sec
118172 Start 2: PythonTest.greet_john
119- 2/4 Test #2: PythonTest.greet_john ............ Passed 0.47 sec
173+ 2/4 Test #2: PythonTest.greet_john ................ Passed 0.47 sec
120174 Start 3: PythonTest.greet_julia
121- 3/4 Test #3: PythonTest.greet_julia ........... Passed 0.47 sec
122- Start 4: PythonTest.greet_michael
123- 4/4 Test #4: PythonTest.greet_michael ...... ... Passed 0.54 sec
175+ 3/4 Test #3: PythonTest.greet_julia ............... Passed 0.47 sec
176+ Start 4: PythonTest.subfolder. greet_michael
177+ 4/4 Test #4: PythonTest.subfolder. greet_michael ... Passed 0.54 sec
124178
125179 It is also possible to regroup all tests under one :term: `CTest ` test, as
126180was the case when :ref: `using the target <tutorial/target >`. This can be
127181useful during development to ensure that the tests run faster, especially
128182if you use :term: `fixtures <fixture> ` with a broader scope.
129183
130- This can be done by setting the ``BUNDLE_TESTS `` argument to True:
184+ This can be done by setting the ``BUNDLE_TESTS `` option to True:
131185
132186.. code-block :: cmake
133- :emphasize-lines: 9
187+ :emphasize-lines: 8
134188
135189 pytest_discover_tests(
136190 PythonTest
137191 LIBRARY_PATH_PREPEND
138192 $<TARGET_FILE_DIR:MyLibrary>
139193 PYTHON_PATH_PREPEND
140194 $<TARGET_FILE_DIR:MyLibrary>
141- TRIM_FROM_NAME "^test_"
142195 DEPENDS MyLibrary
143196 BUNDLE_TESTS True
144197 )
145198
146- After re-building the project, running :term: `CTest ` will display the tests as
147- follows:
199+ After rebuilding the project once again , running :term: `CTest ` will display the
200+ tests as follows:
148201
149202.. code-block :: console
150203
@@ -154,5 +207,5 @@ follows:
154207 .. note ::
155208
156209 The :envvar: `BUNDLE_PYTHON_TESTS ` environment variable can also set this
157- argument dynamically.
210+ option dynamically.
158211
0 commit comments