Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expect multiple failures in google test?

Tags:

c++

googletest

How can I expect multiple failures in google test? I use this when testing that asserts happen in my code under test. Because these asserts are not fatal, multiple can happen.

The following testcase reproduces this:

void failTwice()
{
   EXPECT_TRUE(false) << "fail first time";
   EXPECT_TRUE(false) << "fail second time";
}

TEST_F(FailureTest, testMultipleFails)
{
   EXPECT_NONFATAL_FAILURE(failTwice(), "time");
}

This produces the following output:

gtest/src/gtest.cc:657: Failure
Expected: 1 non-fatal failure
  Actual: 2 failures
FailureTest.h:20: Non-fatal failure:
Value of: false
  Actual: false
Expected: true
fail first time

FailureTest.h:20: Non-fatal failure:
Value of: false
  Actual: false
Expected: true
fail second time

The problem is this: Expected: 1 non-fatal failure

How can I tell google test to expect multiple failures?

like image 302
Thirler Avatar asked Jun 04 '14 12:06

Thirler


People also ask

How do I turn off Google test?

The docs for Google Test 1.7 suggest: If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).

What is a death test?

Death Tests are test assertions to put in unit test code, just like ASSERT_EQ , that check that the program exists or crashes in a certain manner given some inputs. So in the program code we still use assert() or other traditional methods to close/crash the application.

Does Gtest run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

How do you write a value parameterized test?

To write value-parameterized tests, first you should define a fixture class. It must be derived from both testing::Test and testing::WithParamInterface<T> (the latter is a pure interface), where T is the type of your parameter values.

What happens if a Google test assertion fails?

If a Google Test assertion fails, it will print an error message and throw an exception, which will be treated as a failure by your host testing framework. If you compile your code with exceptions disabled, a failed Google Test assertion will instead exit your program with a non-zero code, which will also signal a test failure to your test runner.

What is the difference between tests and failures in Google Test?

The testsattribute of a <testsuites>or <testsuite>element tells how many test functions the Google Test program or test case contains, while the failuresattribute tells how many of them failed. The timeattribute expresses the duration of the test, test case, or entire test program in milliseconds.

Is it possible for a unit test to be expected to fail?

It would be unusual to have a unit test in an expected-to-fail state. Unit tests can test for positive conditions ("expect x to equal 2 ") or negative conditions ("expect save to throw an exception if name is null "), and can be flagged not to run at all (if the feature is pending and you don't want the noise in your test output).

What's new in Google Test?

GoogleTest Docs Now that you have read Primerand learned how to write tests using Google Test, it's time to learn some new tricks. This document will show you more assertions as well as how to construct complex failure messages, propagate fatal failures, reuse and speed up your test fixtures, and use various flags with your tests. More Assertions


1 Answers

I had the same problem and the one approach that works is:

EXPECT_NONFATAL_FAILURE({
    EXPECT_NONFATAL_FAILURE(failTwice(), "");
},"Actual: 2");

With the "Actual: 2" I set the expectation that 2 non-fatal failures will occur. One drawback is that it is not easy to tell which error messages you are expecting.

like image 53
ForcaeLuz Avatar answered Sep 20 '22 20:09

ForcaeLuz