Skip to content

Commit

Permalink
Catch2: handle exceptions (#117)
Browse files Browse the repository at this point in the history
Exceptions are a separate output in the XML from normal failures.
  • Loading branch information
jnewb1 committed Sep 16, 2023
1 parent bd997b4 commit 78d8966
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# UNRELEASED

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

# 2.4.0 (2023-09-08)

Expand Down
13 changes: 13 additions & 0 deletions src/pytest_cpp/catch2.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ def _parse_xml(
fail_msg,
)
)
test_exception = test_case.findall(".//Exception")
for exception in test_exception:
file_name = exception.attrib["filename"]
line_num = int(exception.attrib["line"])

fail_msg = f"Error: {exception.text}"
failures.append(
(
file_name,
line_num,
fail_msg,
)
)
skipped = False # TODO: skipped tests don't appear in the results
result.append((test_name, failures, skipped))

Expand Down
2 changes: 2 additions & 0 deletions tests/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ for filename in boost_files:
for env, label in [(c3env, "_v3"), (c2env, "")]:
catch2_success = env.Object(f'catch2_success{label}', 'catch2_success.cpp')
catch2_failure = env.Object(f'catch2_failure{label}', 'catch2_failure.cpp')
catch2_error = env.Object(f'catch2_error{label}', 'catch2_error.cpp')

env.Program(catch2_success)
env.Program(catch2_failure)
env.Program(catch2_error)

SConscript('acceptance/googletest-samples/SConscript')
SConscript('acceptance/boosttest-samples/SConscript')
Expand Down
10 changes: 10 additions & 0 deletions tests/catch2_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

#include <stdexcept>


TEST_CASE( "Error" ) {
throw std::runtime_error("a runtime error");
REQUIRE(true);
}
6 changes: 6 additions & 0 deletions tests/test_pytest_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,12 @@ def test_catch2_failure(exes):
)
assert len(failures) == 2

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


class TestError:
def test_get_whitespace(self):
Expand Down

0 comments on commit 78d8966

Please sign in to comment.