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

[question] One setup for multiple tests #160

Closed
Morales1235 opened this issue Nov 11, 2018 · 2 comments
Closed

[question] One setup for multiple tests #160

Morales1235 opened this issue Nov 11, 2018 · 2 comments

Comments

@Morales1235
Copy link

Morales1235 commented Nov 11, 2018

I wanna to do tests of two functions with one setup. According to instructions I made TEST_CASE with SUBCASEs (also tried with SCENARIO and WHEN-THEN-THEN), but this makes my setup being runned with every subcase. I don't know what do I do wrong.

main

#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest.h>
//my includes...
//#define DOCTEST_CONFIG_DISABLE
int main(int argc, char* argv[])
{
    int result = 0;
#ifdef DOCTEST_CONFIG_DISABLE
    result = MyProgram(argc, argv);
#else
    doctest::Context testContext(argc, argv);
    result = testContext.run();
    if(testContext.shouldExit())
        return result;
#endif

    return result;
}

*.cpp with tested functions

TEST_CASE("OPTIONAL - prediction test")
{
    SUBCASE("")
   {
           //setup...
	    SUBCASE("func1")
	    {
		CHECK(function1);
	    }
	    SUBCASE("func2")
	    {
		CHECK(function2);
	    }
    }
}

According to found slides it should run setup only once, but it runs before sunc1 and second time before func2

@Morales1235 Morales1235 changed the title [question] [pproblem] One setup for multiple tests - multiple runs [question] One setup for multiple tests Nov 11, 2018
@onqtam
Copy link
Member

onqtam commented Nov 11, 2018

Hi! Actually the setup should indeed be ran multiple times in this case - see the relevant docs - and also here is the slide from my doctest presentation - the setup is indeed being ran multiple times because the test case is re-entered multiple times (depending on the number of leaf subcase nodes there are - think of it as a nested tree of subcases and a DFS-ish traversal).

So if you want the setup to be ran just once and have 2 functions checked after it - just do this:

TEST_CASE("OPTIONAL - prediction test")
{
    //setup...
    CHECK(function1);
    CHECK(function2);
}

Does this help?

@Morales1235
Copy link
Author

Thank You for reply,
I saw that links; watching this slide again... I was wrong, eh. :-) Don't know why I was convinced to have one setup in this configuration.
I was wondering to have msg to every check, that's why I wanted to use case for every check. But after a while of reflection I found that few check in one case are sufficient - There is enough info.

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