Skip to content

Commit bdd7575

Browse files
committed
Update documentation
1 parent 7f0593f commit bdd7575

6 files changed

Lines changed: 146 additions & 52 deletions

File tree

doc/api_reference.rst

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ API Reference
1414
pytest_discover_tests(NAME
1515
[WORKING_DIRECTORY dir]
1616
[TRIM_FROM_NAME pattern]
17+
[TRIM_FROM_FULL_NAME pattern]
1718
[LIBRARY_PATH_PREPEND path1 path2...]
1819
[PYTHON_PATH_PREPEND path1 path2...]
1920
[ENVIRONMENT env1 env2...]
2021
[DEPENDS target1 target2...]
22+
[INCLUDE_FILE_PATH]
2123
[STRIP_PARAM_BRACKETS]
2224
[BUNDLE_TESTS]
2325
)
@@ -35,11 +37,21 @@ API Reference
3537
Specify the directory in which to run the :term:`Pytest` command. If
3638
this option is not provided, the current source directory is used.
3739

40+
* ``INCLUDE_FILE_PATH``
41+
42+
Include the file path of each collected :term:`Pytest` test into its generated
43+
test name. This helps distinguish tests with the same class or function names
44+
by including their source file for clearer identification::
45+
46+
pytest_discover_tests(
47+
...
48+
INCLUDE_FILE_PATH
49+
)
50+
3851
* ``TRIM_FROM_NAME``
3952

40-
Specify a `regular expression
41-
<https://en.wikipedia.org/wiki/Regular_expression>`_ to trim part of the
42-
class, method and function names discovered before creating the test.
53+
Specify a :term:`regular expression` to trim part of each
54+
class, method and function name discovered before creating the test.
4355
This option can be used to trim the convention prefix required by
4456
:term:`Pytest` for discovery::
4557

@@ -48,6 +60,16 @@ API Reference
4860
TRIM_FROM_NAME "^(Test|test_)"
4961
)
5062

63+
* ``TRIM_FROM_FULL_NAME``
64+
65+
Specify a :term:`regular expression` to trim parts of the full test name
66+
generated::
67+
68+
pytest_discover_tests(
69+
...
70+
TRIM_FROM_FULL_NAME "(Test|test_)"
71+
)
72+
5173
* ``STRIP_PARAM_BRACKETS``
5274

5375
Remove square brackets added to the test identifier when using

doc/glossary.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ Glossary
1010

1111
.. seealso:: https://cmake.org/cmake/help/latest/command/add_test.html
1212

13-
Boost
14-
Set of C++ libraries providing features complementary from the standard
15-
library.
16-
17-
.. seealso:: https://www.boost.org/
18-
19-
Boost Python
20-
Component library from :term:`Boost` which is a framework for
21-
interfacing Python and C++.
22-
23-
.. seealso:: https://wiki.python.org/moin/boost.python
24-
2513
CMake
2614
CMake is a compilation configuration platform that manages the build
2715
process in an operating system and in a compiler-independent manner.
@@ -50,13 +38,25 @@ Glossary
5038

5139
.. seealso:: https://docs.pytest.org/en/latest/explanation/fixtures.html
5240

41+
nanobind
42+
Nanobind is a fast, minimalistic C++ library designed to create Python
43+
bindings for C++ code.
44+
45+
.. seealso:: https://nanobind.readthedocs.io/en/latest/index.html
46+
5347
parametrizing tests
5448
Parameterized tests in :term:`Pytest` enable the execution of a single test function
5549
with multiple sets of input parameters, facilitating the evaluation of different
5650
test cases within a unified test structure.
5751

5852
.. seealso:: https://docs.pytest.org/en/stable/how-to/parametrize.html
5953

54+
regular expression
55+
A regular expression is a sequence of characters that defines a search pattern,
56+
typically used for pattern matching and manipulation within strings.
57+
58+
.. seealso:: https://en.wikipedia.org/wiki/Regular_expression
59+
6060
Pip
6161
Pip is a package-management system written for Python.
6262

doc/integration.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ able to discover the newly installed configuration automatically using its
4343
option should not be set to False.
4444

4545
When using a Python virtual environment, or if Python is installed in a
46-
non-standard location, the :envvar:`CMAKE_PREFIX_PATH` environment variable
46+
non-standard location, the :envvar:`Pytest_ROOT` environment variable
4747
(or :term:`CMake` option) can be used to guide the discovery process::
4848

49-
cmake -S . -B ./build -D "CMAKE_PREFIX_PATH=/path/to/python/prefix"
49+
cmake -S . -B ./build -D "Pytest_ROOT=/path/to/python/prefix"
5050

5151
This is also necessary when installing the configuration in the
5252
`Python user directory
@@ -61,10 +61,11 @@ Building and testing example project
6161

6262
An example project has been made available to test the configuration.
6363

64-
Ensure that a minimal version of :term:`Boost Python` is available, and
65-
install :term:`Pytest` with its :term:`CMake` configuration as described in the
66-
:ref:`previous section <installing>`. Then build the example::
64+
Ensure that :term:`nanobind` is available, and install :term:`Pytest` with its
65+
:term:`CMake` configuration as described in the :ref:`previous section <installing>`.
66+
Then build the example::
6767

68+
export nanobind_ROOT=$(python -m nanobind --cmake_dir)
6869
cmake -S ./example -B ./build
6970
cmake --build ./build
7071

doc/release/release_notes.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ Release Notes
66

77
.. release:: Upcoming
88

9+
.. change:: new
10+
11+
Added ``INCLUDE_FILE_PATH`` option to the :func:`pytest_discover_tests`
12+
function use the file path to compute the test identifier.
13+
14+
.. seealso:: :ref:`tutorial/function`
15+
16+
.. change:: new
17+
18+
Added ``TRIM_FROM_FULL_NAME`` option to the :func:`pytest_discover_tests`
19+
function trim parts of the full test name generated.
20+
21+
.. seealso:: :ref:`tutorial/function`
22+
23+
.. change:: fixed
24+
25+
Fixed the ``BUNDLE_TESTS`` option to the :func:`pytest_discover_tests`
26+
function which was poorly implemented.
27+
928
.. change:: changed
1029

1130
Replace Boost.Python with nanobind for the example module.
@@ -17,7 +36,7 @@ Release Notes
1736

1837
.. change:: new
1938

20-
Added ``STRIP_PARAM_BRACKETS`` argument to the :func:`pytest_discover_tests`
39+
Added ``STRIP_PARAM_BRACKETS`` option to the :func:`pytest_discover_tests`
2140
function to strip square brackets used for :term:`parametrizing tests`.
2241

2342
.. seealso:: :ref:`tutorial/function`
@@ -160,7 +179,7 @@ Release Notes
160179

161180
.. change:: new
162181

163-
Added ``ENVIRONMENT`` argument to the :func:`pytest_discover_tests`
182+
Added ``ENVIRONMENT`` option to the :func:`pytest_discover_tests`
164183
function to provide custom environment variables during the tests.
165184

166185
.. seealso:: :ref:`tutorial/function`

doc/tutorial.rst

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
Tutorial
55
********
66

7-
Once :ref:`integrated in your project <integration>`, the ``Pytest::Pytest``
7+
Once :ref:`integrated into your project <integration>`, the ``Pytest::Pytest``
88
target and the :func:`pytest_discover_tests` function are available for using.
99

1010
.. _tutorial/target:
1111

1212
Using 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
2929
to 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`.
5757
If all tests are successful, the output will look as follows:
5858

5959
.. code-block:: console
@@ -74,7 +74,7 @@ as failed.
7474
Using 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`
7878
tests for each Python test collected. Therefore, the configuration added in the
7979
previous 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**
9493
target.
9594

9695
The 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
9897
environment variable used to locate shared libraries will be automatically
9998
chosen 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
112166
follows:
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
126180
was the case when :ref:`using the target <tutorial/target>`. This can be
127181
useful during development to ensure that the tests run faster, especially
128182
if 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

example/src/python/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ nanobind_add_module(pyFoo main.cpp)
33
set_target_properties(pyFoo
44
PROPERTIES
55
PREFIX ""
6+
SUFFIX ".so"
67
OUTPUT_NAME foo
78
)
89

910
if(WIN32)
1011
set_target_properties(pyFoo PROPERTIES SUFFIX ".pyd")
11-
elseif(APPLE)
12-
set_target_properties(pyFoo PROPERTIES SUFFIX ".so")
1312
endif()
1413

1514
target_link_libraries(pyFoo PUBLIC foo)

0 commit comments

Comments
 (0)