Skip to content

Commit

Permalink
log contexts for failures in JUnit reporter (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
runave authored Dec 3, 2020
1 parent c19e04c commit 2d51525
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
16 changes: 15 additions & 1 deletion doctest/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4990,7 +4990,6 @@ namespace {
}

// TODO:
// - log_contexts()
// - log_message()
// - respond to queries
// - honor remaining options
Expand Down Expand Up @@ -5186,12 +5185,27 @@ DOCTEST_CLANG_SUPPRESS_WARNING_POP
<< line(rb.m_line) << (opt.gnu_file_line ? ":" : "):") << std::endl;

fulltext_log_assert_to_stream(os, rb);
log_contexts(os);
testCaseData.addFailure(rb.m_decomp.c_str(), assertString(rb.m_at), os.str());
}

void log_message(const MessageData&) override {}

void test_case_skipped(const TestCaseData&) override {}

void log_contexts(std::ostringstream& s) {
int num_contexts = get_num_active_contexts();
if(num_contexts) {
auto contexts = get_active_contexts();

s << " logged: ";
for(int i = 0; i < num_contexts; ++i) {
s << (i == 0 ? "" : " ");
contexts[i]->stringify(&s);
s << std::endl;
}
}
}
};

DOCTEST_REGISTER_REPORTER("junit", 0, JUnitReporter);
Expand Down
16 changes: 15 additions & 1 deletion doctest/parts/doctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,6 @@ namespace {
}

// TODO:
// - log_contexts()
// - log_message()
// - respond to queries
// - honor remaining options
Expand Down Expand Up @@ -2542,12 +2541,27 @@ DOCTEST_CLANG_SUPPRESS_WARNING_POP
<< line(rb.m_line) << (opt.gnu_file_line ? ":" : "):") << std::endl;

fulltext_log_assert_to_stream(os, rb);
log_contexts(os);
testCaseData.addFailure(rb.m_decomp.c_str(), assertString(rb.m_at), os.str());
}

void log_message(const MessageData&) override {}

void test_case_skipped(const TestCaseData&) override {}

void log_contexts(std::ostringstream& s) {
int num_contexts = get_num_active_contexts();
if(num_contexts) {
auto contexts = get_active_contexts();

s << " logged: ";
for(int i = 0; i < num_contexts; ++i) {
s << (i == 0 ? "" : " ");
contexts[i]->stringify(&s);
s << std::endl;
}
}
}
};

DOCTEST_REGISTER_REPORTER("junit", 0, JUnitReporter);
Expand Down
2 changes: 2 additions & 0 deletions examples/all_features/test_output/abort_after_junit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
coverage_maxout.cpp(0):
CHECK( str.compare(const_str, true) != 0 ) is NOT correct!
values: CHECK( 0 != 0 )
logged: should fail

</failure>
<failure message="0 != 0" type="CHECK">
coverage_maxout.cpp(0):
CHECK( str.compare("omgomgomg", false) != 0 ) is NOT correct!
values: CHECK( 0 != 0 )
logged: should fail

</failure>
</testcase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
coverage_maxout.cpp(0):
CHECK( str.compare(const_str, true) != 0 ) is NOT correct!
values: CHECK( 0 != 0 )
logged: should fail

</failure>
<failure message="0 != 0" type="CHECK">
coverage_maxout.cpp(0):
CHECK( str.compare("omgomgomg", false) != 0 ) is NOT correct!
values: CHECK( 0 != 0 )
logged: should fail

</failure>
<failure message="false" type="CHECK">
coverage_maxout.cpp(0):
CHECK( len_is_zero ) is NOT correct!
values: CHECK( false )
logged: should fail

</failure>
<failure message="Approx( 7.0 ) == 5" type="CHECK">
Expand Down
14 changes: 14 additions & 0 deletions examples/all_features/test_output/logging.cpp_junit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@
logging.cpp(0):
CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
values: CHECK( 1 != 1 )
logged: current iteration of loop:
i := 0

</failure>
<failure message="2 != 2" type="CHECK">
logging.cpp(0):
CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
values: CHECK( 2 != 2 )
logged: current iteration of loop:
i := 1

</failure>
<failure message="4 != 4" type="CHECK">
logging.cpp(0):
CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
values: CHECK( 4 != 4 )
logged: current iteration of loop:
i := 2

</failure>
<failure message="8 != 8" type="CHECK">
logging.cpp(0):
CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
values: CHECK( 8 != 8 )
logged: current iteration of loop:
i := 3

</failure>
<failure message="16 != 16" type="CHECK">
logging.cpp(0):
CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
values: CHECK( 16 != 16 )
logged: current iteration of loop:
i := 4

</failure>
</testcase>
Expand All @@ -38,6 +48,10 @@ CHECK( vec[i] != (1 &lt;&lt; i) ) is NOT correct!
logging.cpp(0):
CHECK( some_var == 666 ) is NOT correct!
values: CHECK( 42 == 666 )
logged: someTests() returned: 42
this should be printed if an exception is thrown even if no assert has failed: 42
in a nested scope this should be printed as well: 42
why is this not 666 ?!

</failure>
<error message="exception">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ I am a test from the executable!
plugin.cpp(0):
CHECK( 1 == 2 ) is NOT correct!
values: CHECK( 1 == 2 )
logged: some info

</failure>
</testcase>
Expand Down

0 comments on commit 2d51525

Please sign in to comment.