Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure reporting should print out previous SECTIONs for data-driven testing #734

Closed
jktjkt opened this issue Oct 21, 2016 · 4 comments
Closed

Comments

@jktjkt
Copy link

jktjkt commented Oct 21, 2016

If a testcase differs only in the amount of input data, it would be nice to use SECTION to pass these bits into the object under test, like the following example. However, because the REQUIRE stanza is outside of the nested SECTIONs, error messages do not contain references to the failing sections.

TEST_CASE("check that Foo detect invalid input") {
  std::string input;
  SECTION("no data") {
  }
  SECTION("garbage") {
    input = "xxx";
  }
  SECTION("just two words") {
    input = "foo bar";
  }
  Foo foo;
  foo.setLogLevel(666);
  REQUIRE(!foo.parse(input));
}

This can be worked around by adding additional macros, but it quickly gets ugly when the test involves more than few statements.

TEST_CASE("check that Foo detect invalid input") {
  #define DOIT(...) \
    Foo foo;\
    foo.setLogLevel(666); \
    REQUIRE(!foo.parse(__VA_ARGS__));

  SECTION("garbage") {
    DOIT("xxx")
  }
  SECTION("just two words") {
    DOIT("foo bar")
  }
}

I would like Catch to keep track of those SECTIONs which have been already closed, and to add their names into the test output if expectations fail.

jruffin added a commit to jruffin/Catch that referenced this issue Oct 28, 2016
The Console reporter now "traces" the full section stack of its
current test execution and prints it out instead of the section
stack of the failing assertion itself. Likewise, the JUnit reporter
now groups assertions together into the <testcase> of the test
execution where they happened.

This fixes catchorg#734 and allows post-condition tests after the deepest
SECTION in a test execution.
@jruffin
Copy link

jruffin commented Oct 28, 2016

+1. Many of my tests are either made of common code and REQUIREs that are run against a bunch of input data with corresponding expected output data, or have common post-condition checks that might only fail in a particular run but be OK in the others. On top of that, I use a mocking framework that checks whether everything was called as expected during the destruction of its mock objects at the end...

So far, I have been working around the issue by packing most common code and REQUIREs into fixture methods and calling those in each SECTION that needs it and using a listener to trace and keep the deepest section stack of a run and print it when the mocking framework fails, but neither are nice nor very "catchy" in my opinion.

I have managed to modify Catch to fix the issue with both reporters where this seems sensible (console and JUnit), see #736.

jktjkt pushed a commit to jktjkt/Catch that referenced this issue Jan 6, 2017
The Console reporter now "traces" the full section stack of its
current test execution and prints it out instead of the section
stack of the failing assertion itself. Likewise, the JUnit reporter
now groups assertions together into the <testcase> of the test
execution where they happened.

This fixes catchorg#734 and allows post-condition tests after the deepest
SECTION in a test execution.
@philsquared
Copy link
Collaborator

For the record I agree with this request and it's on my queue

@textshell
Copy link
Contributor

Any news on this? This would be quite useful to have.

@horenmar
Copy link
Member

horenmar commented Apr 21, 2019

Since generators have been added to Catch, you should either use them for data driven testing, or you make your own reporter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants