Indicating Pass/Fail in a Test Method: Difference between revisions
Jump to navigation
Jump to search
Line 54: | Line 54: | ||
===void return=== | ===void return=== | ||
This is the return type that is most often used as it provides the most convenience. | This is the return type that is most often used as it provides the most convenience. | ||
c++ examples are shown, but techniques are applicable to c as well. | |||
<source lang=c> | <source lang=c> | ||
#include <srtest.h> | #include <srtest.h> | ||
void Test1() | void MyTest::Test1() | ||
{ | { | ||
int sum = DoAdd(2, 2); | int sum = DoAdd(2, 2); | ||
Line 66: | Line 68: | ||
} | } | ||
void Test2() | void MyTest::Test2() | ||
{ | { | ||
int sum = DoAdd(3, 3); | int sum = DoAdd(3, 3); | ||
Line 72: | Line 74: | ||
// using the macro, but same result | // using the macro, but same result | ||
srTestStatus_e eStatus = (sum == 6) ? srTEST_PASS : srTEST_FAIL; | srTestStatus_e eStatus = (sum == 6) ? srTEST_PASS : srTEST_FAIL; | ||
testCase.SetStatus(eStatus, 0); | |||
} | } | ||
void Test3() | void MyTest::Test3() | ||
{ | { | ||
// status is not set, so default srTEST_PASS is in force | // status is not set, so default srTEST_PASS is in force | ||
Line 85: | Line 85: | ||
===integer return=== | ===integer return=== | ||
<source lang=c> | |||
#include <srtest.h> | |||
int MyTest::Test1() | |||
{ | |||
int sum = DoAdd(2, 2); | |||
// return true -> PASS, false -> FAIL | |||
return (sum == 4); | |||
} | |||
int MyTest::Test2() | |||
{ | |||
int sum = DoAdd(3, 3); | |||
// | |||
srEXPECT_EQ(sum, 6); | |||
// return value does not affect test status since the status | |||
// was set explicitly using a test macro above | |||
return true; | |||
} | |||
</source> | |||
===bool return=== | ===bool return=== |
Revision as of 21:06, 8 February 2012
Test Method Return Values
Stride test methods (or free functions in the case of scl_flist test units) must conform to certain criteria. These criteria allow for several choices in return type from the test method.
Return Type | Description | How Return Value is Interpreted | Default Status |
void | Most common return type | Since there is no return, PASS/FAIL status is set in the body of the method using a
or a runtime call |
|
integer type | Integer types include:
may include signed/unsigned/const qualifiers |
|
|
bool | c++ only |
|
|
Examples
The following example implementations validate the operation of a function named DoAdd() (not shown). Required header file declarations are assumed to be in place.
void return
This is the return type that is most often used as it provides the most convenience.
c++ examples are shown, but techniques are applicable to c as well.
#include <srtest.h>
void MyTest::Test1()
{
int sum = DoAdd(2, 2);
// test macro validates condition and sets test status.
// the srEXPECT macros continue
srEXPECT_EQ(sum, 4);
}
void MyTest::Test2()
{
int sum = DoAdd(3, 3);
// set the status using a runtime call; much less convenient than
// using the macro, but same result
srTestStatus_e eStatus = (sum == 6) ? srTEST_PASS : srTEST_FAIL;
testCase.SetStatus(eStatus, 0);
}
void MyTest::Test3()
{
// status is not set, so default srTEST_PASS is in force
}
integer return
#include <srtest.h>
int MyTest::Test1()
{
int sum = DoAdd(2, 2);
// return true -> PASS, false -> FAIL
return (sum == 4);
}
int MyTest::Test2()
{
int sum = DoAdd(3, 3);
//
srEXPECT_EQ(sum, 6);
// return value does not affect test status since the status
// was set explicitly using a test macro above
return true;
}
bool return
#include <srtest.h>
bool MyTest::Test1()
{
int sum = DoAdd(2, 2);
// return true -> PASS, false -> FAIL
return (sum == 4);
}
bool MyTest::Test2()
{
int sum = DoAdd(3, 3);
//
srEXPECT_EQ(sum, 6);
// return value does not affect test status since the status
// was set explicitly using a test macro above
return true;
}