Test Macros: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
(No difference)

Revision as of 22:55, 5 May 2010

Test 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 Test Macros

There are four types of macros: EXPECT, ASSERT, EXIT, and NOTE macros. They have slight but important differences in their behavior.

srEXPECT_xx(condition)

  • If the condition does match the expectation, this macro:
    • sets the current test case status to PASS
  • If the condition does not match the expectation, this macro:
    • sets the current test case status to FAIL
    • adds a comment to the current test case's results report which includes the condition as well as file and line number
    • does not alter code execution

srASSERT_xx(condition)

  • If the condition does match the assertion, this macro:
    • sets the current test case status to PASS
  • If the condition does not match the expectation, this macro:
    • sets the current test case status to FAIL
    • adds a comment to the current test case's results report which includes the condition as well as file and line number
    • immediately returns from the current test function. The return specifies no value, therefore a function or method that uses srASSERT_xx should be declared to return void.

srEXIT_xx(condition)

  • The behavior and restrictions of the exit macros are identical to the ASSERT macros. EXIT macros differ only in that they cause the test unit to cease execution. That is, no subsequent test methods within the currently running test unit are executed once an EXIT macro has failed in its assertion. This macro is useful in test units that implement behavioral testing where each test methods depends on the successful completion of the preceding test method.
  • If a teardown fixture is declared, and the srEXIT_xx() fires within a test method, the teardown method will be called.
  • If srEXIT_xx() fires within a scl_test_cclass test unit, its de-init function will be called if declared.

srNOTE(message)

  • Unconditionally adds a comment to the current test case's results report which includes the message as well as file and line number

Assertions

The following sections document the several types of testing macros that are provided by the STRIDE Framework. For simplicity, we refer to all the macros using a prefix tag - when using the macros in test code, the prefix should be replaced by one of the following: srEXPECT, srASSERT, or srEXIT, depending on how the test writer wants failures to be handled.

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. When a failure is detected, the report will be annotated.

Boolean
macro Pass if
prefix_TRUE(cond); cond is non-zero
prefix_FALSE(cond); cond is zero

Example

int a = 5; 
int b = 5; 
srEXPECT_TRUE(a == b); 

srEXPECT_FALSE(2 < 1);

srEXPECT_FALSE(a != b);
srEXPECT_TRUE(1 < 2); 
srEXPECT_TRUE(1);

Comparison Macros

Comparison macros take two operands and compare them using the indicated operator.

The comparison macros will work for scalar types as well as C++ objects that have the corresponding comparison operator implemented.

Comparison
macro Pass if
prefix_EQ(val1, val2); val1 == val2
prefix_NE(val1, val2); val1 != val2
prefix_LT(val1, val2); val1 < val2
prefix_LE(val1, val2); val1 <= val2
prefix_GT(val1, val2); val1 > val2
prefix_GE(val1, val2); val1 >= val2

Example

int a = 5; 
int b = 5; 

srEXPECT_EQ( 4, 4 ); 
srEXPECT_NE( 6, 7 );
srEXPECT_EQ( a, b ); 
srEXPECT_GT( 2 , 1 );
srEXPECT_GE( 2 , 1 );

C String Comparison Macros

C String Comparison Macros are intended only for use with C-style null terminated strings. The strings can be char or wchar_t based.

Don't use these macros to compare C++ objects representing strings since such classes typically 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
macro Pass if
prefix_STREQ(str1, str2); str1 and str2 have the same content
prefix_STRNE(str1, str2); str1 and str2 have different content
prefix_STRCASEEQ(str1, str2); str1 and str2 have the same content, ignoring case.
prefix_STRCASENE(str1, str2); str1 and str2 have different content, ignoring case.

Example

const char* s1 = "This String is unique"; 
const char* s2 = "This String has an equivalent"; 
const char* s2Twin = "This String has an equivalent"; 
const char* s2TwinNoCase = "this string has an equivalent"; 

srEXPECT_STREQ( s2, s2Twin); 
srEXPECT_STREQ( s2, "This String has an equivalent"); 
srEXPECT_STRCASEEQ(s2, s2TwinNoCase); 

srEXPECT_STRNE(s1, s2);  
srEXPECT_STRNE(s2, s2TwinNoCase); 
srEXPECT_STRCASENE(s1, s2);


Predicate Macros

Predicate macros allow user control over the pass/fail decision making.

A predicate is a function returning bool that is implemented by the user that is the macro. Up to four arguments can also passed to the predicate through the macro.

Predicates
macro Pass if
prefix_PRED1(pred, val1) pred(val1) returns true
prefix_PRED2(pred, val1, val2) pred(val1, val2) returns true
…(up to arity of 4)

Example

static int alwaysTrueOneArg(int i)
{
    return 1; 
}

static int alwaysTrueTwoArgs(int i, int j)
{
    return 1;
}

static void Pass()
{
    // examples of passing expectations using predicates.

    srEXPECT_PRED1(alwaysTrueOneArg, 25); 
    srEXPECT_PRED2(alwaysTrueTwoArgs, 100, 33); 
}

Floating Point Comparison Macros

Floating point macros are for comparing equivalence (or near equivalence) of floating point numbers.

These macros are are very useful for dealing with floating point round-off effects.

Floating Point comparison
macro Pass if
prefix_NEAR(val1, val2, epsilon); The absolute value of the difference between val1 and val2 is less than or equal to epsilon.

Example

float x =           2.00005f;
float nearX =       2.00006f; 
float nearXEpsilon = .000019f;

double y =           1.2345; 
double nearY =       1.23456; 
double nearYEpsilon = .00019;
    
srEXPECT_NEAR(x, nearX, nearXEpsilon); 
srEXPECT_NEAR(y, nearY, nearYEpsilon);

Exception Macros

Exception macros are used to ensure that expected exceptions are thrown. They are applicable to C++ code only.

These macros require exception support from the target compiler. If the target compiler does not have exception support the macros cannot be used.

Exceptions
macro Pass if
prefix_THROW(statement, ex_type); statement throws an exception of type ex_type
prefix_THROW_ANY(statement); statement throws an exception (type not important)
prefix_NO_THROW(statement); statement does not throw an exception
Example
srEXPECT_THROW(throwStdException(), std::exception); 
srEXPECT_THROW(throwInt(), int);

srEXPECT_THROW_ANY(throwStdException()); 
srEXPECT_THROW_ANY(throwInt()); 

srEXPECT_NO_THROW(doesntThrow());

Dynamic Test Case Macros

The macros presented so far assume that their actions are directed at the currenly in-scope test case. However, test cases can be created dynamically using STRIDE's [Runtime Test Services].

In order to handle dynamic test cases, each of the macros requires another parameter which specifies 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).

macro
Boolean
prefix_TRUE_DYN(tc, cond);
prefix_FALSE_DYN(tc, cond);
Comparison
prefix_EQ_DYN(tc, val1, val2);
prefix_NE_DYN(tc, val1, val2);
prefix_LT_DYN(tc, val1, val2);
prefix_LE_DYN(tc, val1, val2);
prefix_GT_DYN(tc, val1, val2);
prefix_GE_DYN(tc, val1, val2);
C-string comparison
prefix_STREQ_DYN(tc, str1, str2);
prefix_STRNE_DYN(tc, str1, str2);
prefix_STRCASEEQ_DYN(tc, str1, str2);
prefix_STRCASENE_DYN(tc, str1, str2);
Exceptions
prefix_THROW_DYN(statement, ex_type);
prefix_THROW_ANY_DYN(tc, statement);
prefix_NO_THROW_DYN(tc, statement);
Predicates
prefix_PRED1_DYN(tc, pred, val1);
prefix_PRED2_DYN(tc, pred, vall, val2);
…(up to arity of 4)
Floating Point
prefix_NEAR_DYN(tc, val1, val2, epsilon);

Logging

Note macros provide a simple means to add annotations attached to the currently executing test case. These annotations are added to the test report info with a level of either Error, Warning, or Info according to the macro that is used.

Error Logging
srNOTE_ERROR(message) message is a pointer to a null-terminated string
srNOTE[1..9]_ERROR(message, ...) message is a pointer to a null-terminated format string

... variable list (up to 9) matching the format string

Warning Logging
srNOTE_WARN(message) message is a pointer to a null-terminated string
srNOTE[1..9]_WARN(message, ...) message is a pointer to a null-terminated format string

... variable list (up to 9) matching the format string

Info Logging
srNOTE_INFO(message) message is a pointer to a null-terminated string
srNOTE[1..9]_INFO(message, ...) message is a pointer to a null-terminated format string

... variable list (up to 9) matching the format string

  • versions of these log macros also exist for use with dynamically generated test cases. To use these macros, just append _DYN to the macro names shown above and then pass the explicit testCaseHandle_t item as the first argument to the macros.
  • The maximum length of the message string approximately 1000 characters. If the maximum length is exceeded, the message string is truncated.

Example

srNOTE_ERROR("This is an error message.");
srNOTE1_WARN("This is a warning message with format string %d.", 123);
srNOTE2_INFO("This is an info message with format string %s and %s.", "this", "that");

C++ Only Features

Use Operator << to Augment Report Annotation

In C++ test code all macros support adding to a test's annotations using the << operator. For example:

srEXPECT_TRUE(a != b) << "My custom message";

For more information please read this.