Skip to content

Commit cdb37a3

Browse files
authored
Initial Implementation for supporting EXTRA_ARGS for command line options for tests (#53)
1 parent 4e45d40 commit cdb37a3

6 files changed

Lines changed: 59 additions & 2 deletions

File tree

cmake/FindPytest.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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.

cmake/PytestAddTests.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff 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.

test/06-extra-args/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
)

test/06-extra-args/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
)

0 commit comments

Comments
 (0)