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

Is possible call a TEST_CASE like a function? #666

Closed
p4535992 opened this issue May 24, 2016 · 4 comments
Closed

Is possible call a TEST_CASE like a function? #666

p4535992 opened this issue May 24, 2016 · 4 comments

Comments

@p4535992
Copy link

Hi, i just started using Catch, so forgive me if this is a dumb question or i miss some reference on the documentation, i want to call from a int main(){...} function under the 'src' folder the TEST_CASE allocate under the 'test' folder, like this:
File src/main.cpp

#include "catch.hpp"
int main(int argc, char **argv) {

    // Call CATCH TEST on a different cpp file ( this is a pseudo code)    
    TEST_CASE("testRectangle()")

    // OTHER THINGS 
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

File test/TestRectangle.cpp

#include "catch.hpp"
#include "Rectangle.h"

TEST_CASE("testRectangle()","[Rectangle]" ){
    Point p1(2, 3);
    Point p2(4, 8);
    Rectangle r1(p1, p2);
    REQUIRE(r1.getTopLeft() == p1);
    REQUIRE(r1.getBottomRight() == p2);
    Shape* pS = new Rectangle();
    delete pS;
    pS = new Rectangle(p1, p2);
    delete pS;
}

TEST_CASE("testDraw() ","[Rectangle]" ){
    Point p1(2, 3);
    Point p2(4, 8);
    Rectangle::Rectangle r1(p1, p2);
    bool result = r1.draw();
    REQUIRE(result==true);
}

Is possible doing that?
Ty in advance.

@czipperz
Copy link

Catch already registers tests and runs them automatically with TEST_CASE. See the tutorial:

Put this in main.cpp:

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

If you want to do stuff before / after the tests are run, see own-main:

#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

int main( int argc, char* const argv[] )
{
  // global setup...

  int result = Catch::Session().run( argc, argv );

  // global clean-up...

  return result;
}

If you look at the source code for TEST_CASE, it creates a semi-random function name. Use the following to be able to call these test functions from others (which you shouldn't do probably):

#include "catch.hpp"
void testRectangle() {
...
}

// global variable!
namespace {
Catch::AutoReg closer(&testRectangle, CATCH_INTERNAL_LINEINFO,
                      Catch::NameAndDesc("testRectangle()", "[Rectangle]"));
}

@p4535992
Copy link
Author

Ty very much , i accept this.

@PureAbstract
Copy link
Contributor

@czipperz "it creates a semi-random function name"

Strictly speaking, it's not at all random - it's entirely deterministic.

@czipperz
Copy link

Yes, it's just based on the line number, but that is unpredictable when you edit the file

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

3 participants