File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
5858 cmake_parse_arguments (
5959 PARSE_ARGV 1 "" "STRIP_PARAM_BRACKETS;INCLUDE_FILE_PATH;BUNDLE_TESTS"
6060 "WORKING_DIRECTORY;TRIM_FROM_NAME;TRIM_FROM_FULL_NAME"
61- "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS;EXTRA_ARGS"
61+ "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS;EXTRA_ARGS;DISCOVERY_EXTRA_ARGS "
6262 )
6363
6464 # Set platform-specific library path environment variable.
@@ -133,6 +133,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
133133 -D "TEST_PROPERTIES=${_PROPERTIES} "
134134 -D "CTEST_FILE=${_tests_file} "
135135 -D "EXTRA_ARGS=${_EXTRA_ARGS} "
136+ -D "DISCOVERY_EXTRA_ARGS=${_DISCOVERY_EXTRA_ARGS} "
136137 -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR} /PytestAddTests.cmake" )
137138
138139 # Create a custom target to run the tests.
Original file line number Diff line number Diff line change @@ -27,6 +27,10 @@ if(CMAKE_SCRIPT_MODE_FILE)
2727 endforeach ()
2828
2929 # Handle EXTRA_ARGS for individual tests
30+ if (BUNDLE_TESTS)
31+ list (PREPEND EXTRA_ARGS ${DISCOVERY_EXTRA_ARGS} )
32+ endif ()
33+
3034 set (EXTRA_ARGS_WRAPPED)
3135 foreach (arg IN LISTS EXTRA_ARGS)
3236 list (APPEND EXTRA_ARGS_WRAPPED "[==[${arg} ]==]" )
@@ -69,7 +73,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
6973 execute_process (
7074 COMMAND "${PYTEST_EXECUTABLE} "
7175 --collect-only -q
72- --rootdir=${WORKING_DIRECTORY} .
76+ --rootdir=${WORKING_DIRECTORY} ${DISCOVERY_EXTRA_ARGS} .
7377 OUTPUT_VARIABLE _output_lines
7478 ERROR_VARIABLE _output_lines
7579 OUTPUT_STRIP_TRAILING_WHITESPACE
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ API Reference
2424 [STRIP_PARAM_BRACKETS]
2525 [BUNDLE_TESTS]
2626 [EXTRA_ARGS arg1 arg2...]
27+ [DISCOVERY_EXTRA_ARGS arg1 arg2...]
2728 )
2829
2930 The options are:
@@ -200,6 +201,26 @@ API Reference
200201 `Pytest Command-Line Flags
201202 <https://docs.pytest.org/en/stable/reference/reference.html#command-line-flags> `_
202203
204+ * ``DISCOVERY_EXTRA_ARGS ``
205+
206+ List of extra arguments to pass on the :term: `Pytest ` command line for
207+ the test discovery::
208+
209+ pytest_discover_tests(
210+ ...
211+ DISCOVERY_EXTRA_ARGS "-k=expression"
212+ )
213+
214+ .. seealso ::
215+
216+ `Pytest Command-Line Flags
217+ <https://docs.pytest.org/en/stable/reference/reference.html#command-line-flags> `_
218+
219+ .. warning ::
220+
221+ This option may affect test discovery. Some arguments can change
222+ output format and break the test collection logic. Use with caution!
223+
203224 .. note ::
204225
205226 This function works similarly to the `gtest_discover_tests
Original file line number Diff line number Diff line change 44Release Notes
55*************
66
7+ .. release :: Upcoming
8+
9+ .. change :: changed
10+
11+ Added ``DISCOVERY_EXTRA_ARGS `` option to the
12+ :func: `pytest_discover_tests ` function, allowing custom arguments to be
13+ passed to the :term: `Pytest ` command during test collection.
14+ Thanks :github_user: `AmeyaVS `!
15+
716.. release :: 0.12.0
817 :date: 2025-02-09
918
Original file line number Diff line number Diff line change @@ -9,5 +9,23 @@ enable_testing()
99pytest_discover_tests (
1010 TestExtraArgs
1111 EXTRA_ARGS "--capture=no" "--cmdopt=demo"
12- DEPENDS test_extra_args.py
12+ )
13+
14+ pytest_discover_tests (
15+ TestDiscoveryExtraArgs.IgnoreCustomArgs
16+ EXTRA_ARGS "--capture=no"
17+ DISCOVERY_EXTRA_ARGS "--ignore=custom_args"
18+ )
19+
20+ pytest_discover_tests (
21+ TestDiscoveryExtraArgs.OnlyCustomArgs
22+ EXTRA_ARGS "--cmdopt=demo"
23+ DISCOVERY_EXTRA_ARGS "-k=custom"
24+ )
25+
26+ pytest_discover_tests (
27+ TestDiscoveryExtraArgs.OnlyCustomArgsBundled
28+ EXTRA_ARGS "--cmdopt=demo"
29+ DISCOVERY_EXTRA_ARGS "-k=custom"
30+ BUNDLE_TESTS
1331)
Original file line number Diff line number Diff line change 1- import pytest
2-
31def pytest_addoption (parser ):
42 parser .addoption (
53 "--cmdopt" , action = "store" , help = "custom options"
64 )
7-
8- @pytest .fixture
9- def cmdopt (request ):
10- return request .config .getoption ("--cmdopt" )
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ def test_requires_cmdopt (request ):
4+ """Fails unless '--cmdopt=<value>' is passed to pytest."""
5+ if request .config .getoption ("--cmdopt" ) is None :
6+ pytest .fail ("Test requires '--cmdopt' to properly execute the test." )
7+ assert True
Original file line number Diff line number Diff line change @@ -5,12 +5,3 @@ def test_requires_no_capture(request):
55 if "--capture=no" not in request .config .invocation_params .args :
66 pytest .fail ("Test requires '--capture=no' to properly display output." )
77 assert True
8-
9-
10- def test_requires_args (cmdopt ):
11- """Fails unless '--cmdopt=<value>' is passed to pytest."""
12- if cmdopt != None :
13- print (f"Option Value: cmdopt: { cmdopt } " )
14- else :
15- pytest .fail ("Test requires '--cmdopt=<value>' to properly execute the test." )
16- pass
You can’t perform that action at this time.
0 commit comments