Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmake/FindPytest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
cmake_parse_arguments(
PARSE_ARGV 1 "" "STRIP_PARAM_BRACKETS;INCLUDE_FILE_PATH;BUNDLE_TESTS"
"WORKING_DIRECTORY;TRIM_FROM_NAME;TRIM_FROM_FULL_NAME"
"LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS"
"LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS;EXTRA_ARGS"
)

# Set platform-specific library path environment variable.
Expand Down Expand Up @@ -132,6 +132,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
-D "ENVIRONMENT=${_ENVIRONMENT}"
-D "TEST_PROPERTIES=${_PROPERTIES}"
-D "CTEST_FILE=${_tests_file}"
-D "EXTRA_ARGS=${_EXTRA_ARGS}"
-P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PytestAddTests.cmake")

# Create a custom target to run the tests.
Expand Down
9 changes: 8 additions & 1 deletion cmake/PytestAddTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ if(CMAKE_SCRIPT_MODE_FILE)
list(APPEND ENCODED_ENVIRONMENT "${env}")
endforeach()

# Handle EXTRA_ARGS for individual tests
set(EXTRA_ARGS_WRAPPED)
foreach(arg IN LISTS EXTRA_ARGS)
list(APPEND EXTRA_ARGS_WRAPPED "[==[${arg}]==]")
endforeach()
list(JOIN EXTRA_ARGS_WRAPPED " " EXTRA_ARGS_STR)

# Macro to create individual tests with optional test properties.
macro(create_test NAME IDENTIFIER)
string(APPEND _content
"add_test([==[${NAME}]==] \"${PYTEST_EXECUTABLE}\" [==[${IDENTIFIER}]==])\n"
"add_test([==[${NAME}]==] \"${PYTEST_EXECUTABLE}\" [==[${IDENTIFIER}]==] ${EXTRA_ARGS_STR} )\n"
)

# Prepare the properties for the test, including the environment settings.
Expand Down
13 changes: 13 additions & 0 deletions test/06-extra-args/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.20)

project(TestExtraArgs)

find_package(Pytest REQUIRED)

enable_testing()

pytest_discover_tests(
TestExtraArgs
EXTRA_ARGS "--capture=no" "--cmdopt=demo"
DEPENDS test_extra_args.py
)
10 changes: 10 additions & 0 deletions test/06-extra-args/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

def pytest_addoption(parser):
parser.addoption(
"--cmdopt", action="store", help="custom options"
)

@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
16 changes: 16 additions & 0 deletions test/06-extra-args/test_extra_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

def test_requires_no_capture(request):
"""Fails unless '--capture=no' is passed to pytest."""
if "--capture=no" not in request.config.invocation_params.args:
pytest.fail("Test requires '--capture=no' to properly display output.")
assert True


def test_requires_args(cmdopt):
"""Fails unless '--cmdopt=<value>' is passed to pytest."""
if cmdopt != None:
print(f"Option Value: cmdopt: {cmdopt}")
else:
pytest.fail("Test requires '--cmdopt=<value>' to properly execute the test.")
pass
10 changes: 10 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ ExternalProject_Add(
-C Release -VV
-R TestProperties.Validate
)

ExternalProject_Add(
TestExtraArgs
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/06-extra-args
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/_deps/06-extra-args
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR>
INSTALL_COMMAND ""
TEST_COMMAND ${CMAKE_CTEST_COMMAND}
-C Release -VV
)
Loading