Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch an assert with Google test?

I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test?

Example code under test:

int factorial(int n){     assert(n >= 0);     //.... } 

And then the test:

#include <gtest/gtest.h> TEST(FactorialTest,assertNegative){     EXPECT_ANY_THROW({          factorial(-1);     }); } 

But EXPECT_ANY_THROW doesn't catch the assert but only exceptions. I'm searching for a solution to catch asserts.

like image 253
Killrazor Avatar asked Sep 21 '10 00:09

Killrazor


People also ask

How does Google Test work?

Independent and Repeatable: Googletest isolates the tests by running each of them on a different object. Portable and Reusable: Googletest works on different Oses (Linux, Windows, or a Mac), with different compilers. When tests fail, it should provide as much information about the problem as possible.

Does Google Test 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).

When to Use assert vs expect?

ASSERT: Fails fast, aborting the current function. EXPECT: Continues after the failure.


1 Answers

Google test provides ASSERT_DEATH, EXPECT_DEATH and other related macros.

This question and What are Google Test, Death Tests are each other's answers. Does that make them duplicates, or not? ;-)

like image 91
Steve Jessop Avatar answered Oct 09 '22 01:10

Steve Jessop