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

Customize test names / variable substitution #284

Closed
dhardtke opened this issue Sep 3, 2019 · 5 comments
Closed

Customize test names / variable substitution #284

dhardtke opened this issue Sep 3, 2019 · 5 comments

Comments

@dhardtke
Copy link

dhardtke commented Sep 3, 2019

Description

This might be a feature request as I have not found any way to do this using existing features.

Suppose I have a test case like this:

TEST_CASE ("Dynamic values") {
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> dist(1, 21);

    for (int i = 0; i < 20; i++) {
        int n = dist(rng);
        int reference = fib(n);
        CHECK_MESSAGE(fibonacci(n) == reference, "fibonacci(" << n << ") should be " << reference);
    }
}

When the test is executed I get ERROR: CHECK( fibonacci(n) == reference ) is NOT correct!. Is there any way to substitute the n variable in the message?

@onqtam
Copy link
Member

onqtam commented Sep 5, 2019

Doesn't the code you pasted work? Isn't there something like this on the next 2 lines after what you showed:

test.cpp:6: ERROR: CHECK( fibonacci(n) == reference ) is NOT correct!
  values: CHECK( 123 == 146 )
  logged: fibonacci(n) should be...

Where n is exactly what you care about? (123 and 146 are totally random - I made them up)

@dhardtke
Copy link
Author

dhardtke commented Sep 5, 2019

I am using the XML reporter and the test name there is just fibonacci(n) == reference.
I am trying to find a way to make the n be substituted in the test name.

@onqtam
Copy link
Member

onqtam commented Sep 5, 2019

For this example from doctest: https://github.com/onqtam/doctest/blob/master/examples/all_features/logging.cpp#L20
The XML output is the following: https://github.com/onqtam/doctest/blob/master/examples/all_features/test_output/logging.cpp_xml.txt#L6

Note the <Info> after <Original> and <Expanded>

Isn't there something like that in your xml output?

@onqtam
Copy link
Member

onqtam commented Sep 5, 2019

I can't think of a way to add n as an integer in the assert string representation.

What the CHECK_MESSAGE(cond, msg << var << msg2); expands to is the following:

{
    INFO(msg << var << msg2);
    CHECK(cond);
}

and in the report doctest will stringify cond by using the # preprocessor operator and will also stringify the value of cond, but if cond is a function(arg) it will only stringify the result of that function and not the argument as well.

INFO() is the only mechanism in doctest for logging additional information and that is what's behind CHECK_MESSAGE and the output which I showed previously is the only thing which is possible with the framework. Let me know if you have any further questions.

@onqtam
Copy link
Member

onqtam commented Sep 8, 2019

@dhardtke I'm thinking of closing this issue soon - let me know if there are any further questions.

@dhardtke dhardtke closed this as completed Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants