Test Macros: Difference between revisions
(New page: == Pass/Fail Macros == The STRIDE Test Unit implementation also provides a set of Test Macros (declared in srtest.h) available for use within test methods. The macros are optional - you a...) |
|||
Line 5: | Line 5: | ||
The macros can be used in C++ and C test unit code (Note that there is no C version of exceptions test). | The macros can be used in C++ and C test unit code (Note that there is no C version of exceptions test). | ||
== General guidelines for all macros == | |||
srEXPECT_xx macros | '''srEXPECT_xx''' macros | ||
* set the current test case status to FAIL and produce an annotation in the report if the expectation fails. If the expectation succeeds, there is no action. | |||
srASSERT_xx macros will set the current test case to fail (if it hasn’t already been set) and insert an annotation into the report if the assertion fails. In addition, a return from the current function will occur. srASSERT_xx macros can only be used in test functions which return void. If the assertion succeeds there is no action. | '''srASSERT_xx''' macros will set the current test case to fail (if it hasn’t already been set) and insert an annotation into the report if the assertion fails. In addition, a return from the current function will occur. srASSERT_xx macros can only be used in test functions which return void. If the assertion succeeds there is no action. | ||
srLOG macro will add a new comment to the current test case and produce an annotation in the report with specified level of importance. | srLOG macro will add a new comment to the current test case and produce an annotation in the report with specified level of importance. |
Revision as of 19:22, 3 April 2009
Pass/Fail Macros
The STRIDE Test Unit implementation also provides a set of Test Macros (declared in srtest.h) available for use within test methods. The macros are optional - you are not required to use them in your test units. They provide shortcuts for testing assertions and automatic report annotation in the case of failures.
The macros can be used in C++ and C test unit code (Note that there is no C version of exceptions test).
General guidelines for all macros
srEXPECT_xx macros
- set the current test case status to FAIL and produce an annotation in the report if the expectation fails. If the expectation succeeds, there is no action.
srASSERT_xx macros will set the current test case to fail (if it hasn’t already been set) and insert an annotation into the report if the assertion fails. In addition, a return from the current function will occur. srASSERT_xx macros can only be used in test functions which return void. If the assertion succeeds there is no action.
srLOG macro will add a new comment to the current test case and produce an annotation in the report with specified level of importance.
The report annotation produced by a failed macro always includes the source file and line along with details about the condition that failed and the failing values.
Boolean Macros
The boolean macros take a single condition expression, cond, that evaluates to an integral type or bool. The condition will be evaluated once. if cond evaluates to non-zero the assertion or expectation fails. When a failure occurs the report will be annotated.
Boolean | ||
srEXPECT_TRUE(cond); | srASSERT_TRUE(cond); | cond is true |
srEXPECT_FALSE(cond); | srASSERT_FALSE(cond); | cond is false |
Comparison Macros
Comparison macros take two operands and compare them using the indicated operator. The comparison macros will work for primitive types as well as objects that have the corresponding comparison operator implemented.
Comparison | ||
srEXPECT_EQ(val1, val2); | srASSERT_EQ(val1, val2); | val1 == val2 |
srEXPECT_NE(val1, val2); | srASSERT_NE(val1, val2); | val1 != val2 |
srEXPECT_LT(val1, val2); | srASSERT_LT(val1, val2); | val1 < val2 |
srEXPECT_LE(val1, val2); | srASSERT_LE(val1, val2); | val1 <= val2 |
srEXPECT_GT(val1, val2); | srASSERT_GT(val1, val2); | val1 > val2 |
srEXPECT_GE(val1, val2); | srASSERT_GE(val1, val2); | val1 >= val2 |
C String Comparison Macros
C String Comparison Macros are intended only for use with C-style zero terminated strings. The strings can be char or wchar_t based. In particular, these macros should not be used for object of one or other string class, since such classes have overloaded comparison operators. The standard comparison macros should be used instead.
- An empty string will appear in error message output as “”. A null string will appear as NULL with no surrounding quotes. Otherwise all output strings are quoted.
- The type of str1 and str2 must be compatible with const char* or const wchar_t*.
C-string comparison | ||
srEXPECT_STREQ(str1, str2); | srASSERT_STREQ(str1, str2); | str1 and str2 have the same content |
srEXPECT_STRNE(str1, str2); | srASSERT_STRNE(str1, str2); | str1 and str2 have different content |
srEXPECT_STRCASEEQ(str1, str2); | srASSERT_STRCASEEQ(str1, str2); | str1 and str2 have the same content, ignoring case. |
srEXPECT_STRCASENE(str1, str2); | srASSERT_STRCASENE(str1, str2); | str1 and str2 have different content, ignoring case. |
Exception Macros
Exception macros are used to ensure that expected exceptions are thrown. They require exception support from the target compiler. If the target compiler does not have exception support the macros cannot be used and must be disabled.
Exceptions | ||
srEXPECT_THROW(statement, ex_type); | srASSERT_THROW(statement, ex_type); | statement throws an exception of type ex_type |
srEXPECT_THROW_ANY(statement); | srASSERT_THROW_ANY(statement); | statement throws an exception (type not important) |
srEXPECT_NO_THROW(statement); | srASSERT_NO_THROW(statement); | statement does not throw an exception |
Predicate Macros
Predicate macros allow user control over the pass/fail decision making in a macro. A predicate is a function returning bool that is implemented by the user but passed to the macro. Other arguments for the predicate are also passed to the macro. The macros allow for predicate functions with up to four parameters.
Predicates | ||
srEXPECT_PRED1(pred, val1) | srASSERT_PRED1(pred, val1) | pred(val1) returns true |
srEXPECT_PRED2(pred, val1, val2) | srASSERT_PRED2(pred, val1, val2) | pred(val1, val2) returns true |
…(up to arity of 4) |
All predicate macros require a predicate function function which returns bool. The predicate macros allow functions with one to 4 parameters. Following are the report annotations resulting from expectation or assertion failures.
Floating Point Comparison Macros
Floating point macros are for comparing equivalence (or near equivalence) of floating point numbers. These macros are necessary since because equivalence comparisons for floating point numbers will often fail due to round-off errors.
Floating Point comparison | ||
srEXPECT_NEAR(val1, val2, epsilon); | srASSERT_NEAR(val1, val2, epsilon); | The absolute value of the difference between val1 and val2 is epsilon. |
Dynamic Test Case Macros
The macros presented so far are not capable of dealing with dynamic test cases. In order to handle dynamic test cases, each of the macros requires another parameter which is the test case to report against. Other than this, these macros provide exactly equivalent functionality to the non-dynamic peer. The dynamic macros are listed below. All require a test case, value of type srTestCaseHandle_t from srtest.h, to be passed as the first parameter).
Nonfatal assertion | Fatal Assertion |
Boolean | |
srEXPECT_TRUE_DYN(tc, cond); | srASSERT_TRUE_DYN(tc, cond); |
srEXPECT_FALSE_DYN(tc, cond); | srASSERT_FALSE_DYN(tc, cond); |
Comparison | |
srEXPECT_EQ_DYN(tc, val1, val2); | srASSERT_EQ_DYN(tc, expect, val); |
srEXPECT_NE_DYN(tc, val1, val2); | srASSERT_NE_DYN(tc, val1, val2); |
srEXPECT_LT_DYN(tc, val1, val2); | srASSERT_LT_DYN(tc, val1, val2); |
srEXPECT_LE_DYN(tc, val1, val2); | srASSERT_LE_DYN(tc, val1, val2); |
srEXPECT_GT_DYN(tc, val1, val2); | srASSERT_GT_DYN(tc, val1, val2); |
srEXPECT_GE_DYN(tc, val1, val2); | srASSERT_GE_DYN(tc, val1, val2); |
C-string comparison | |
srEXPECT_STREQ_DYN(tc, str1, str2); | srASSERT_STREQ_DYN(tc, str1, str2); |
srEXPECT_STRNE_DYN(tc, str1, str2); | srASSERT_STRNE_DYN(tc, str1, str2); |
srEXPECT_STRCASEEQ_DYN(tc, str1, str2); | srASSERT_STRCASEEQ_DYN(tc, str1, str2); |
srEXPECT_STRCASENE_DYN(tc, str1, str2); | srASSERT_STRCASENE_DYN(tc, str1, str2); |
Exceptions | |
srEXPECT_THROW_DYN(statement, ex_type); | srASSERT_THROW_DYN(tc, statement, ex_type); |
srEXPECT_THROW_ANY_DYN(tc, statement); | srASSERT_THROW_ANY_DYN(tc, statement); |
srEXPECT_NO_THROW_DYN(tc, statement); | srASSERT_NO_THROW_DYN(tc, statement); |
Predicates | |
srEXPECT_PRED1_DYN(tc, pred, val1); | srASSERT_PRED1_DYN(tc, pred, val1); |
srEXPECT_PRED2_DYN(tc, pred, vall, val2); | srASSERT_PRED2_DYN(tc, pred, vall, val2); |
…(up to arity of 4) | |
Floating Point | |
srEXPECT_NEAR_DYN(tc, val1, val2, epsilon); | srASSERT_NEAR_DYN(tc, val1, val2, epsilon); |
Logging | |
srLOG_DYN(tc, level, message); |
Use operator << for report annotation (C++ tests only)
In C++ test code all macros support the adding to the report annotations using the << operator. For example:
srEXPECT_TRUE(a != b) << "My custom message";
As delivered, the macros will support stream input annotations for all numeric types, "C" string (char* or wchar_t*) and types allowing implicit cast to numeric type or "C" string. The user may overload the << operator in order to annotate reports using any custom type. An example is below.
The following will compile and execute successfully given that the << operator is overloaded as shown:
#include <srtest.h>
// MyCustomClass implementation
class MyCustomClass
{
public:
MyCustomClass(int i) : m_int(i) {}
private:
int m_int;
friend stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj);
};
stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj)
{
ss << obj.m_int;
return ss;
}
void test()
{
MyCustomClass custom(34);
srEXPECT_FALSE(true) << custom;
}