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"
61+ "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS;EXTRA_ARGS "
6262 )
6363
6464 # Set platform-specific library path environment variable.
@@ -132,6 +132,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
132132 -D "ENVIRONMENT=${_ENVIRONMENT} "
133133 -D "TEST_PROPERTIES=${_PROPERTIES} "
134134 -D "CTEST_FILE=${_tests_file} "
135+ -D "EXTRA_ARGS=${_EXTRA_ARGS} "
135136 -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR} /PytestAddTests.cmake" )
136137
137138 # Create a custom target to run the tests.
Original file line number Diff line number Diff line change @@ -26,10 +26,17 @@ if(CMAKE_SCRIPT_MODE_FILE)
2626 list (APPEND ENCODED_ENVIRONMENT "${env} " )
2727 endforeach ()
2828
29+ # Handle EXTRA_ARGS for individual tests
30+ set (EXTRA_ARGS_WRAPPED)
31+ foreach (arg IN LISTS EXTRA_ARGS)
32+ list (APPEND EXTRA_ARGS_WRAPPED "[==[${arg} ]==]" )
33+ endforeach ()
34+ list (JOIN EXTRA_ARGS_WRAPPED " " EXTRA_ARGS_STR)
35+
2936 # Macro to create individual tests with optional test properties.
3037 macro (create_test NAME IDENTIFIER )
3138 string (APPEND _content
32- "add_test([==[${NAME} ]==] \" ${PYTEST_EXECUTABLE} \" [==[${IDENTIFIER} ]==])\n "
39+ "add_test([==[${NAME} ]==] \" ${PYTEST_EXECUTABLE} \" [==[${IDENTIFIER} ]==] ${EXTRA_ARGS_STR} )\n "
3340 )
3441
3542 # Prepare the properties for the test, including the environment settings.
Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.20 )
2+
3+ project (TestExtraArgs)
4+
5+ find_package (Pytest REQUIRED )
6+
7+ enable_testing ()
8+
9+ pytest_discover_tests (
10+ TestExtraArgs
11+ EXTRA_ARGS "--capture=no" "--cmdopt=demo"
12+ DEPENDS test_extra_args.py
13+ )
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ def pytest_addoption (parser ):
4+ parser .addoption (
5+ "--cmdopt" , action = "store" , help = "custom options"
6+ )
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_no_capture (request ):
4+ """Fails unless '--capture=no' is passed to pytest."""
5+ if "--capture=no" not in request .config .invocation_params .args :
6+ pytest .fail ("Test requires '--capture=no' to properly display output." )
7+ 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
Original file line number Diff line number Diff line change @@ -52,3 +52,13 @@ ExternalProject_Add(
5252 -C Release -VV
5353 -R TestProperties.Validate
5454)
55+
56+ ExternalProject_Add (
57+ TestExtraArgs
58+ SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} /06-extra-args
59+ BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} /_deps/06-extra-args
60+ BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR >
61+ INSTALL_COMMAND ""
62+ TEST_COMMAND ${CMAKE_CTEST_COMMAND}
63+ -C Release -VV
64+ )
You can’t perform that action at this time.
0 commit comments