How do I mark a Google Test as "expected failure"?

I want to add a test file for functionality that has not yet been implemented, and mark this test case as "it is normal that I fail."

Is there any way to do this?

EDIT: I want the test to run, and the framework must verify that it does not work while the test file is in the expected failure state.

EDIT2: It seems that the function that interests me does not exist in the google test, but it exists in the Boost Unit Test Framework and in the LIT.

+7
c ++ unit-testing googletest
source share
3 answers
+3
source share

I don't know a direct way to do this, but you can fake it something like this:

 try { // do something that should fail EXPECT_TRUE(false); } catch (...) { // return or print a message, etc. } 

In principle, the test fails if it reaches conflicting expectations.

0
source share

It would be unusual to have a unit test in the expected state. Unit tests can check for positive conditions ("expect x to be 2 ") or negative conditions ("wait for save to throw an exception if name is null "), and can be marked as not (if the function is waiting, and you don’t want to to noise in the test output). But what you seem to be asking is a way to nullify a function test while you work on it. This contradicts the tenants of Test Driven Development.

In TDD, you need to write tests that accurately describe what a function should do. If this function has not yet been written, then, by definition, these tests will and should fail. Then you implement the function until all these tests pass one by one. You want all tests to start with an error, and then proceed to transfer. This is, as you know, when your function is complete.

Think about how it would look if you could mark unsuccessful tests as passing, as you suggest: all tests will pass, and everything will look complete if the function does not work. Then, once you are done and the function works as expected, all of a sudden your tests will fail until you log in and untie them. Besides being a weird way of working, this workflow will be very prone to errors and false positives.

-2
source share

All Articles