google c++ testing framework death tests. in many applications, there are assertions that can cause...

11
Google C++ Testing Framework Death Tests

Upload: benedict-bradford

Post on 01-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Google C++ Testing Framework

Death Tests

Page 2: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Death Tests In many applications, there are assertions that can

cause application failure if a condition is not met. ◦ Square root function to check negative input

These sanity checks, which ensure that the program is in a known good state, are there to fail at the earliest possible time after some program state is corrupted. ◦ If the assertion checks the wrong condition, then the

program may proceed in an erroneous state, which could lead to memory corruption, security holes, or worse.

◦ Hence it is vitally important to test that such assertion statements work as expected.

Since these precondition checks cause the processes to die, we call such tests death tests. ◦ More generally, any test that checks that a program

terminates in an expected fashion is also a death test.

Page 3: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Test Square Root

Page 4: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Test negative input

ASSERT_EXIT(statement, predicate, expected_message)Predefined predicate: ::testing::ExitedWithCode(exit_code). The result of this predicate is true only if the program exits with the same exit_code mentioned in the predicate.

ASSERT_DEATH(statement, expected_message)ASSERT_DEATH is simpler than ASSERT_EXIT; it just compares the error messagein standard error with whatever is the user-expected message.

Page 5: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Tests Execution

Page 6: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Macros Support Death Tests

statement:  a statement that is expected to cause the process to die, predicate: a function or function object that evaluates an integer exit status:  regex is a regular expression that the stderr output of statement is expected to match. Note that statement can be any valid statement(including compound statement) and doesn't have to be an expression.As usual, the ASSERT variants abort the current test function, while the EXPECT variants do not.

Page 7: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

What crash means?means that the process terminates

with a non-zero exit status code. either the process has called exit(non-zero

value) or _exit(non-zero value) , or it may be killed by a signal.

means that if statement terminates the process with a 0 exit code, it is not considered a crash by EXPECT_DEATH. ◦Use EXPECT_EXIT instead if this is the

case, or if you want to restrict the exit code more precisely.

Page 8: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Predicate must accept an int and return a bool. The

death test succeeds only if the predicate returns true.

Page 9: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Death test only cares about three things1. does statement abort or exit the process?2. (in the case

of ASSERT_EXIT and EXPECT_EXIT) does the exit status satisfy predicate? Or (in the case of ASSERT_DEATH and EXPECT_DEATH) is the exit status non-zero? And

3. does the stderr output match regex?

Page 10: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square
Page 11: Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square

Regular Expression Syntax