Unity Summary.txt 6.5 KB
Newer Older
1
==============
G
greg-williams 已提交
2 3 4
Unity Test API
==============

5 6
[Copyright (c) 2007 - Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams]

G
greg-williams 已提交
7 8 9 10
-------------
Running Tests 
------------- 

11
RUN_TEST(func, linenum)
G
greg-williams 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

Each Test is run within the macro RUN_TEST.  This macro performs necessary setup before the test is called and handles cleanup and result tabulation afterwards.

--------------
Ignoring Tests
--------------

There are times when a test is incomplete or not valid for some reason.  At these times, TEST_IGNORE can be called.  Control will immediately be returned to the caller of the test, and no failures will be returned.

TEST_IGNORE()

Ignore this test and return immediately

TEST_IGNORE_MESSAGE (message)

Ignore this test and return immediately.  Output a message stating why the test was ignored.

--------------
Aborting Tests
--------------

33
There are times when a test will contain an infinite loop on error conditions, or there may be reason to escape from the test early without executing the rest of the test.  A pair of macros support this functionality in Unity.  The first (TEST_PROTECT) sets up the feature, and handles emergency abort cases.  TEST_ABORT can then be used at any time within the tests to return to the last TEST_PROTECT call.
G
greg-williams 已提交
34 35 36 37 38

TEST_PROTECT()

Setup and Catch macro

39
TEST_ABORT()
G
greg-williams 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52

Abort Test macro

Example:

main()
{
    if (TEST_PROTECT() == 0)
    {
        MyTest();
    }
}

53 54 55 56
If MyTest calls TEST_ABORT, program control will immediately return to TEST_PROTECT with a non-zero return value.


=======================
G
greg-williams 已提交
57
Unity Assertion Summary
58
=======================
G
greg-williams 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

--------------------
Basic Validity Tests
--------------------

TEST_ASSERT_TRUE(condition)

Evaluates whatever code is in condition and fails if it evaluates to false

TEST_ASSERT_FALSE(condition)

Evaluates whatever code is in condition and fails if it evaluates to true

TEST_ASSERT(condition)

Another way of calling TEST_ASSERT_TRUE

TEST_ASSERT_UNLESS(condition)

Another way of calling TEST_ASSERT_FALSE

80 81
TEST_FAIL()
TEST_FAIL_MESSAGE(message)
G
greg-williams 已提交
82 83 84 85 86 87 88 89

This test is automatically marked as a failure.  The message is output stating why.

------------------------------
Numerical Assertions: Integers
------------------------------

TEST_ASSERT_EQUAL_INT(expected, actual)
90 91 92 93
TEST_ASSERT_EQUAL_INT8(expected, actual)
TEST_ASSERT_EQUAL_INT16(expected, actual)
TEST_ASSERT_EQUAL_INT32(expected, actual)
TEST_ASSERT_EQUAL_INT64(expected, actual)
G
greg-williams 已提交
94

95 96 97
Compare two integers for equality and display errors as signed integers. A cast will be performed
to your natural integer size so often this can just be used.  When you need to specify the exact size,
like when comparing arrays, you can use a specific version:
G
greg-williams 已提交
98 99 100



101
TEST_ASSERT_EQUAL_UINT(expected, actual)	
G
greg-williams 已提交
102

103 104
Compare two integers for equality and display errors as unsigned integers.  Like INT, there are 
variants for different sizes also.
G
greg-williams 已提交
105 106 107



108
TEST_ASSERT_EQUAL_HEX(expected, actual)
G
greg-williams 已提交
109

110 111 112
Compares two integers for equality and display errors as hexadecimal.  Like the other integer comparisons,
you can specify the size... here the size will also effect how many nibbles are shown (for example, HEX16 
will show 4 nibbles).
G
greg-williams 已提交
113

114
_ARRAY
G
greg-williams 已提交
115

116 117 118
You can append _ARRAY to any of these macros to make an array comparison of that type.  Here you will
need to care a bit more about the actual size of the value being checked.  You will also specify an
additional argument which is the number of elements to compare.  For example:
G
greg-williams 已提交
119

120
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, elements)
M
mvandervoord 已提交
121 122


123
TEST_ASSERT_EQUAL(expected, actual)
M
mvandervoord 已提交
124

125
Another way of calling TEST_ASSERT_EQUAL_INT
M
mvandervoord 已提交
126 127 128



129
TEST_ASSERT_INT_WITHIN(delta, expected, actual)
M
mvandervoord 已提交
130

131 132
Asserts that the actual value is within plus or minus delta of the expected value.  This also comes in 
size specific variants.
M
mvandervoord 已提交
133 134


G
greg-williams 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
-----------------------------
Numerical Assertions: Bitwise
-----------------------------

TEST_ASSERT_BITS(mask, expected, actual)

Use an integer mask to specify which bits should be compared between two other integers.  High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BITS_HIGH(mask, actual)

Use an integer mask to specify which bits should be inspected to determine if they are all set high.  High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BITS_LOW(mask, actual)

Use an integer mask to specify which bits should be inspected to determine if they are all set low.  High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BIT_HIGH(bit, actual)

Test a single bit and verify that it is high.  The bit is specified 0-31 for a 32-bit integer.

TEST_ASSERT_BIT_LOW(bit, actual)

Test a single bit and verify that it is low.  The bit is specified 0-31 for a 32-bit integer.

----------------------------
Numerical Assertions: Floats
----------------------------

TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual)

Asserts that the actual value is within plus or minus delta of the expected value.

167 168 169 170
TEST_ASSERT_EQUAL_FLOAT(expected, actual)

Asserts that two floating point values are "equal" within a small % delta of the expected value.

G
greg-williams 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
-----------------
String Assertions
-----------------

TEST_ASSERT_EQUAL_STRING(expected, actual)

Compare two null-terminate strings.  Fail if any character is different or if the lengths are different.

TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message)

Compare two null-terminate strings.  Fail if any character is different or if the lengths are different. Output a custom message on failure.

------------------
Pointer Assertions
------------------

Most pointer operations can be performed by simply using the integer comparisons above.  However, a couple of special cases are added for clarity.

TEST_ASSERT_NULL(pointer)

Fails if the pointer is not equal to NULL

TEST_ASSERT_NOT_NULL(pointer)

Fails if the pointer is equal to NULL

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

-----------------
Memory Assertions
-----------------

TEST_ASSERT_EQUAL_MEMORY(expected, actual, len)

Compare two blocks of memory.  This is a good generic assertion for types that can't be coerced into acting like
standard types... but since it's a memory compare, you have to be careful that your data types are packed.

--------
_MESSAGE
--------

you can append _MESSAGE to any of the macros to make them take an additional argument.  This argument
is a string that will be printed at the end of the failure strings.  This is useful for specifying more
information about the problem.