All About xUnit
Assertions
In every assert you indicate the value you expect.
embUnit will compare it to the actual value from the code under test.
The code in the tabs below shows how to assert information about various types.embUnit will compare it to the actual value from the code under test.
You can assert that two strings are equal as follows:
static void testEqualityOfTwoStrings(void)
{
char expected[] = "Something";
char actual[] = "Something";
ASSERT_EQ_STR(expected, actual);
}static void testEqualityOfTwoMoreStrings(void)
{
char* actual = "Something";
ASSERT_EQ_STR("Something", actual);
}
Check that two integers are equal:
static void testIntEquality(void)
{
int actual = 10;
ASSERT_EQ_INT(10, actual);
}static void testUnsignedIntEquality(void)
{
unsigned int expected = 10;
unsigned int actual = 10;
ASSERT_EQ_UINT(expected, actual);
}
Due to their internal representation, it is tricky to say when two
float
s or two double
s are equal.
The canonical solution is to assert equality to within a certain precision, or tolerance:
static void testFloatEqualityGivenPrecision(void)
{
float expected = 10.01F;
float actual = 10.01F;
float precision = 0.000001F;
ASSERT_NEAR_DBL(expected, actual, precision);
}static void testDoubleEqualityGivenPrecision(void)
{
double actual = 10.01;
double precision = 0.000001;
ASSERT_NEAR_DBL(10.01, actual, precision);
}
Check that pointers are
NULL
or not NULL
:
static void testNullPointer(void)
{
char* object = NULL;
ASSERT_NULL(object);
}static void testNonNullPointer(void)
{
char* object = "some text";
ASSERT_NOT_NULL(object);
}
Check that a Boolean expression is true or false:
static void testBooleanTrue(void)
{
int result = 1;
ASSERT_TRUE(result);
}static void testBooleanFalse(void)
{
int result = 0;
ASSERT_FALSE(result);
}
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |