110 likes | 245 Views
Google C++ Testing Framework. Death Tests. 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
E N D
Google C++ Testing Framework Death Tests
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.
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 message in standard error with whatever is the user-expected message.
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.
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.
Predicate • must accept an int and return a bool. The death test succeeds only if the predicate returns true.
Death test only cares about three things • does statement abort or exit the process? • (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 • does the stderr output match regex?