Skip to content

Commit c91dd36

Browse files
corrodedHashbuddly27
authored andcommitted
Allow escaped characters in test names and identifiers
Updated pytest_discover_tests to handle escaped characters in test names and identifiers, ensuring compatibility with parameterized tests that include such characters.
1 parent 93a92e1 commit c91dd36

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

cmake/PytestAddTests.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
2929
# Macro to create individual tests with optional test properties.
3030
macro(create_test NAME IDENTIFIER)
3131
string(APPEND _content
32-
"add_test(\"${NAME}\" \"${PYTEST_EXECUTABLE}\" \"${IDENTIFIER}\")\n"
32+
"add_test([==[${NAME}]==] \"${PYTEST_EXECUTABLE}\" [==[${IDENTIFIER}]==])\n"
3333
)
3434

3535
# Prepare the properties for the test, including the environment settings.
@@ -45,12 +45,12 @@ if(CMAKE_SCRIPT_MODE_FILE)
4545
endforeach()
4646

4747
# Append the test properties to the content.
48-
string(APPEND _content "set_tests_properties(\"${NAME}\" ${args})\n")
48+
string(APPEND _content "set_tests_properties([==[${NAME}]==] ${args})\n")
4949
endmacro()
5050

5151
# If tests are bundled together, create a single test group.
5252
if (BUNDLE_TESTS)
53-
create_test("${TEST_GROUP_NAME}" "${WORKING_DIRECTORY}")
53+
create_test("\${TEST_GROUP_NAME}" "\${WORKING_DIRECTORY}")
5454

5555
else()
5656
# Set environment variables for collecting tests.
@@ -133,7 +133,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
133133
set(test_case "${WORKING_DIRECTORY}/${line}")
134134

135135
# Create the test for CTest.
136-
create_test("${test_name}" "${test_case}")
136+
create_test("\${test_name}" "\${test_case}")
137137
endforeach()
138138

139139
# Warn if no tests were discovered.

doc/release/release_notes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
Release Notes
55
*************
66

7+
.. release:: Upcoming
8+
9+
.. change:: fixed
10+
11+
Updated the :func:`pytest_discover_tests` function to support special
12+
characters in test names and identifiers. Thanks :github_user:`corrodedHash`!
13+
714
.. release:: 0.11.1
815
:date: 2024-10-27
916

test/01-modify-name/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(EXPECTED
2222
"TestModifyName.Simple.test_addition_with_params[5-5-10]"
2323
"TestModifyName.Simple.test_addition_with_params[10-5-15]"
2424
"TestModifyName.Simple.test_addition_with_params[0-0-0]"
25+
"TestModifyName.Simple.test_byte_char_conversion[\\x17]"
2526
)
2627
add_test(NAME TestModifyName.Validate.Simple
2728
COMMAND ${CMAKE_COMMAND}
@@ -57,6 +58,7 @@ set(EXPECTED
5758
"TestModifyName.TrimFromName.addition_with_params[5-5-10]"
5859
"TestModifyName.TrimFromName.addition_with_params[10-5-15]"
5960
"TestModifyName.TrimFromName.addition_with_params[0-0-0]"
61+
"TestModifyName.TrimFromName.byte_char_conversion[\\x17]"
6062
)
6163
add_test(NAME TestModifyName.Validate.TrimFromName
6264
COMMAND ${CMAKE_COMMAND}
@@ -84,6 +86,7 @@ set(EXPECTED
8486
"TestModifyName.StripParamBrackets.test_addition_with_params.5-5-10"
8587
"TestModifyName.StripParamBrackets.test_addition_with_params.10-5-15"
8688
"TestModifyName.StripParamBrackets.test_addition_with_params.0-0-0"
89+
"TestModifyName.StripParamBrackets.test_byte_char_conversion.\\x17"
8790
)
8891
add_test(NAME TestModifyName.Validate.StripParamBrackets
8992
COMMAND ${CMAKE_COMMAND}
@@ -111,6 +114,7 @@ set(EXPECTED
111114
"TestModifyName.IncludeFilePath.test_math_operations.test_addition_with_params[5-5-10]"
112115
"TestModifyName.IncludeFilePath.test_math_operations.test_addition_with_params[10-5-15]"
113116
"TestModifyName.IncludeFilePath.test_math_operations.test_addition_with_params[0-0-0]"
117+
"TestModifyName.IncludeFilePath.test_math_operations.test_byte_char_conversion[\\x17]"
114118
)
115119
add_test(NAME TestModifyName.Validate.IncludeFilePath
116120
COMMAND ${CMAKE_COMMAND}
@@ -139,6 +143,7 @@ set(EXPECTED
139143
"TestModifyName.TrimFromFullName.operations.addition_with_params[5-5-10]"
140144
"TestModifyName.TrimFromFullName.operations.addition_with_params[10-5-15]"
141145
"TestModifyName.TrimFromFullName.operations.addition_with_params[0-0-0]"
146+
"TestModifyName.TrimFromFullName.operations.byte_char_conversion[\\x17]"
142147
)
143148
add_test(NAME TestModifyName.Validate.TrimFromFullName
144149
COMMAND ${CMAKE_COMMAND}

test/01-modify-name/test_math_operations.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ def test_subtraction():
1313
])
1414
def test_addition_with_params(a, b, expected):
1515
assert a + b == expected
16+
17+
@pytest.mark.parametrize("input", [
18+
"\x17",
19+
])
20+
def test_byte_char_conversion(input):
21+
assert ord(input) < 128

0 commit comments

Comments
 (0)