How to Run Specific Test Cases in Googletest

How to run specific test cases in GoogleTest

You could use advanced options to run Google tests.

To run only some unit tests you could use --gtest_filter=Test_Cases1* command line option with value that accepts the * and ? wildcards for matching with multiple tests. I think it will solve your problem.

UPD:

Well, the question was how to run specific test cases. Integration of gtest with your GUI is another thing, which I can't really comment, because you didn't provide details of your approach. However I believe the following approach might be a good start:

  1. Get all testcases by running tests with --gtest_list_tests
  2. Parse this data into your GUI
  3. Select test cases you want ro run
  4. Run test executable with option --gtest_filter

googletest: is it possible to select a single test fixture/case?

you can use the gtests command line option --gtest_filter wehen invoking the executable for the debugging session. @see Running a Subset of the Tests

How to run part of the test cases using gtest

First, you don't need to write a main() for gtest. You can just link with gtest_main library as well as gtest library. It is pretty similar to what you have, though.

Second, to run a specific test, please refer to advanced options.

How can you select specific Google Mock test cases/unit tests to run?

First of all, your unit tests should be lightning fast. Otherwise people are not going to execute them.

As explained in Selecting tests, you cam use --gtest_filter= option. In your specific case : --gtest_filter=ClassAUnitTest.*

Is there a way to stop GoogleTest after a specific test is run, whether it passes or fails?

You have several options:

  1. Simply call exit function at the end of fooTest.

  2. Create a test fixture. In SetUp check for a flag that is always false, but it sets to true if fooTest is executed. Something like this:

bool skip_testing = false;

// Class for test fixture
class MyTestFixture : public ::testing::Test {
protected:
void SetUp() override {
if (skip_testing) {
GTEST_SKIP();
}
}
};

TEST_F(MyTestFixture, test1) {
//
EXPECT_EQ(1, 1);
}

TEST_F(MyTestFixture, footest) {
EXPECT_EQ(1, 1);
skip_testing = true;
}

TEST_F(MyTestFixture, test2) {
//
EXPECT_EQ(1, 1);
}

TEST_F(MyTestFixture, test3) {
//
EXPECT_EQ(1, 1);
}

See a working a working example here: https://godbolt.org/z/8dzKGE6Eh


  1. Similar to Option 2, but use explicit success and failure in SetUp instead of GTEST_SKIP. However using GTEST_SKIP is preferred, cause your output will show that the tests were skipped.

How do we run a single test using Google bazel

On Googletest with the following setup:

TEST(TestSuite, test1)
TEST(TestSuite, test2)

you can isolate test1 using

bazel test --test_arg=--gtest_filter=TestSuite.test $TEST_TARGET_PATH

See --gtest_filter



Related Topics



Leave a reply



Submit