Skip to content

Commit 78d8966

Browse files
authored
Catch2: handle exceptions (#117)
Exceptions are a separate output in the XML from normal failures.
1 parent bd997b4 commit 78d8966

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# UNRELEASED
22

33
- Catch2: add support for catch version 3 ([#115](https://github.com/pytest-dev/pytest-cpp/pull/115))
4+
- Catch2: support exception handling ([#117](https://github.com/pytest-dev/pytest-cpp/pull/117))
45

56
# 2.4.0 (2023-09-08)
67

src/pytest_cpp/catch2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ def _parse_xml(
191191
fail_msg,
192192
)
193193
)
194+
test_exception = test_case.findall(".//Exception")
195+
for exception in test_exception:
196+
file_name = exception.attrib["filename"]
197+
line_num = int(exception.attrib["line"])
198+
199+
fail_msg = f"Error: {exception.text}"
200+
failures.append(
201+
(
202+
file_name,
203+
line_num,
204+
fail_msg,
205+
)
206+
)
194207
skipped = False # TODO: skipped tests don't appear in the results
195208
result.append((test_name, failures, skipped))
196209

tests/SConstruct

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ for filename in boost_files:
4646
for env, label in [(c3env, "_v3"), (c2env, "")]:
4747
catch2_success = env.Object(f'catch2_success{label}', 'catch2_success.cpp')
4848
catch2_failure = env.Object(f'catch2_failure{label}', 'catch2_failure.cpp')
49+
catch2_error = env.Object(f'catch2_error{label}', 'catch2_error.cpp')
4950

5051
env.Program(catch2_success)
5152
env.Program(catch2_failure)
53+
env.Program(catch2_error)
5254

5355
SConscript('acceptance/googletest-samples/SConscript')
5456
SConscript('acceptance/boosttest-samples/SConscript')

tests/catch2_error.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
2+
#include "catch.hpp"
3+
4+
#include <stdexcept>
5+
6+
7+
TEST_CASE( "Error" ) {
8+
throw std::runtime_error("a runtime error");
9+
REQUIRE(true);
10+
}

tests/test_pytest_cpp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ def test_catch2_failure(exes):
628628
)
629629
assert len(failures) == 2
630630

631+
# test exceptions
632+
failures, _ = facade.run_test(exes.get(f"catch2_error{suffix}"), "Error")
633+
[fail1] = failures
634+
assert_catch2_failure(fail1.get_lines()[0], "Error: ", colors)
635+
assert_catch2_failure(fail1.get_lines()[1], "a runtime error", colors)
636+
631637

632638
class TestError:
633639
def test_get_whitespace(self):

0 commit comments

Comments
 (0)