| 1 |
# Unity Test ![CI][] |
| 2 |
|
| 3 |
__Copyright (c) 2007 - 2021 Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams__ |
| 4 |
|
| 5 |
Welcome to the Unity Test Project, one of the main projects of ThrowTheSwitch.org. |
| 6 |
Unity Test is a unit testing framework built for C, with a focus on working with embedded toolchains. |
| 7 |
|
| 8 |
This project is made to test code targetting microcontrollers big and small. |
| 9 |
The core project is a single C file and a pair of headers, allowing it to the added to your existing build setup without too much headache. |
| 10 |
You may use any compiler you wish, and may use most existing build systems including Make, CMake, etc. |
| 11 |
If you'd like to leave the hard work to us, you might be interested in Ceedling, a build tool also by ThrowTheSwitch.org. |
| 12 |
|
| 13 |
If you're new to Unity, we encourage you to tour the [getting started guide][]. |
| 14 |
|
| 15 |
## Getting Started |
| 16 |
|
| 17 |
The [docs][] folder contains a [getting started guide][] and much more tips about using Unity. |
| 18 |
|
| 19 |
## Unity Assertion Summary |
| 20 |
|
| 21 |
For the full list, see [UnityAssertionsReference.md][]. |
| 22 |
|
| 23 |
### Basic Validity Tests |
| 24 |
|
| 25 |
TEST_ASSERT_TRUE(condition) |
| 26 |
|
| 27 |
Evaluates whatever code is in condition and fails if it evaluates to false |
| 28 |
|
| 29 |
TEST_ASSERT_FALSE(condition) |
| 30 |
|
| 31 |
Evaluates whatever code is in condition and fails if it evaluates to true |
| 32 |
|
| 33 |
TEST_ASSERT(condition) |
| 34 |
|
| 35 |
Another way of calling `TEST_ASSERT_TRUE` |
| 36 |
|
| 37 |
TEST_ASSERT_UNLESS(condition) |
| 38 |
|
| 39 |
Another way of calling `TEST_ASSERT_FALSE` |
| 40 |
|
| 41 |
TEST_FAIL() |
| 42 |
TEST_FAIL_MESSAGE(message) |
| 43 |
|
| 44 |
This test is automatically marked as a failure. |
| 45 |
The message is output stating why. |
| 46 |
|
| 47 |
### Numerical Assertions: Integers |
| 48 |
|
| 49 |
TEST_ASSERT_EQUAL_INT(expected, actual) |
| 50 |
TEST_ASSERT_EQUAL_INT8(expected, actual) |
| 51 |
TEST_ASSERT_EQUAL_INT16(expected, actual) |
| 52 |
TEST_ASSERT_EQUAL_INT32(expected, actual) |
| 53 |
TEST_ASSERT_EQUAL_INT64(expected, actual) |
| 54 |
|
| 55 |
Compare two integers for equality and display errors as signed integers. |
| 56 |
A cast will be performed to your natural integer size so often this can just be used. |
| 57 |
When you need to specify the exact size, like when comparing arrays, you can use a specific version: |
| 58 |
|
| 59 |
TEST_ASSERT_EQUAL_UINT(expected, actual) |
| 60 |
TEST_ASSERT_EQUAL_UINT8(expected, actual) |
| 61 |
TEST_ASSERT_EQUAL_UINT16(expected, actual) |
| 62 |
TEST_ASSERT_EQUAL_UINT32(expected, actual) |
| 63 |
TEST_ASSERT_EQUAL_UINT64(expected, actual) |
| 64 |
|
| 65 |
Compare two integers for equality and display errors as unsigned integers. |
| 66 |
Like INT, there are variants for different sizes also. |
| 67 |
|
| 68 |
TEST_ASSERT_EQUAL_HEX(expected, actual) |
| 69 |
TEST_ASSERT_EQUAL_HEX8(expected, actual) |
| 70 |
TEST_ASSERT_EQUAL_HEX16(expected, actual) |
| 71 |
TEST_ASSERT_EQUAL_HEX32(expected, actual) |
| 72 |
TEST_ASSERT_EQUAL_HEX64(expected, actual) |
| 73 |
|
| 74 |
Compares two integers for equality and display errors as hexadecimal. |
| 75 |
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). |
| 76 |
|
| 77 |
TEST_ASSERT_EQUAL(expected, actual) |
| 78 |
|
| 79 |
Another way of calling TEST_ASSERT_EQUAL_INT |
| 80 |
|
| 81 |
TEST_ASSERT_INT_WITHIN(delta, expected, actual) |
| 82 |
|
| 83 |
Asserts that the actual value is within plus or minus delta of the expected value. |
| 84 |
This also comes in size specific variants. |
| 85 |
|
| 86 |
TEST_ASSERT_GREATER_THAN(threshold, actual) |
| 87 |
|
| 88 |
Asserts that the actual value is greater than the threshold. |
| 89 |
This also comes in size specific variants. |
| 90 |
|
| 91 |
TEST_ASSERT_LESS_THAN(threshold, actual) |
| 92 |
|
| 93 |
Asserts that the actual value is less than the threshold. |
| 94 |
This also comes in size specific variants. |
| 95 |
|
| 96 |
### Arrays |
| 97 |
|
| 98 |
_ARRAY |
| 99 |
|
| 100 |
You can append `_ARRAY` to any of these macros to make an array comparison of that type. |
| 101 |
Here you will need to care a bit more about the actual size of the value being checked. |
| 102 |
You will also specify an additional argument which is the number of elements to compare. |
| 103 |
For example: |
| 104 |
|
| 105 |
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, elements) |
| 106 |
|
| 107 |
_EACH_EQUAL |
| 108 |
|
| 109 |
Another array comparison option is to check that EVERY element of an array is equal to a single expected value. |
| 110 |
You do this by specifying the EACH_EQUAL macro. |
| 111 |
For example: |
| 112 |
|
| 113 |
TEST_ASSERT_EACH_EQUAL_INT32(expected, actual, elements) |
| 114 |
|
| 115 |
### Numerical Assertions: Bitwise |
| 116 |
|
| 117 |
TEST_ASSERT_BITS(mask, expected, actual) |
| 118 |
|
| 119 |
Use an integer mask to specify which bits should be compared between two other integers. |
| 120 |
High bits in the mask are compared, low bits ignored. |
| 121 |
|
| 122 |
TEST_ASSERT_BITS_HIGH(mask, actual) |
| 123 |
|
| 124 |
Use an integer mask to specify which bits should be inspected to determine if they are all set high. |
| 125 |
High bits in the mask are compared, low bits ignored. |
| 126 |
|
| 127 |
TEST_ASSERT_BITS_LOW(mask, actual) |
| 128 |
|
| 129 |
Use an integer mask to specify which bits should be inspected to determine if they are all set low. |
| 130 |
High bits in the mask are compared, low bits ignored. |
| 131 |
|
| 132 |
TEST_ASSERT_BIT_HIGH(bit, actual) |
| 133 |
|
| 134 |
Test a single bit and verify that it is high. |
| 135 |
The bit is specified 0-31 for a 32-bit integer. |
| 136 |
|
| 137 |
TEST_ASSERT_BIT_LOW(bit, actual) |
| 138 |
|
| 139 |
Test a single bit and verify that it is low. |
| 140 |
The bit is specified 0-31 for a 32-bit integer. |
| 141 |
|
| 142 |
### Numerical Assertions: Floats |
| 143 |
|
| 144 |
TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) |
| 145 |
|
| 146 |
Asserts that the actual value is within plus or minus delta of the expected value. |
| 147 |
|
| 148 |
TEST_ASSERT_EQUAL_FLOAT(expected, actual) |
| 149 |
TEST_ASSERT_EQUAL_DOUBLE(expected, actual) |
| 150 |
|
| 151 |
Asserts that two floating point values are "equal" within a small % delta of the expected value. |
| 152 |
|
| 153 |
### String Assertions |
| 154 |
|
| 155 |
TEST_ASSERT_EQUAL_STRING(expected, actual) |
| 156 |
|
| 157 |
Compare two null-terminate strings. |
| 158 |
Fail if any character is different or if the lengths are different. |
| 159 |
|
| 160 |
TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) |
| 161 |
|
| 162 |
Compare two strings. |
| 163 |
Fail if any character is different, stop comparing after len characters. |
| 164 |
|
| 165 |
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) |
| 166 |
|
| 167 |
Compare two null-terminate strings. |
| 168 |
Fail if any character is different or if the lengths are different. |
| 169 |
Output a custom message on failure. |
| 170 |
|
| 171 |
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) |
| 172 |
|
| 173 |
Compare two strings. |
| 174 |
Fail if any character is different, stop comparing after len characters. |
| 175 |
Output a custom message on failure. |
| 176 |
|
| 177 |
### Pointer Assertions |
| 178 |
|
| 179 |
Most pointer operations can be performed by simply using the integer comparisons above. |
| 180 |
However, a couple of special cases are added for clarity. |
| 181 |
|
| 182 |
TEST_ASSERT_NULL(pointer) |
| 183 |
|
| 184 |
Fails if the pointer is not equal to NULL |
| 185 |
|
| 186 |
TEST_ASSERT_NOT_NULL(pointer) |
| 187 |
|
| 188 |
Fails if the pointer is equal to NULL |
| 189 |
|
| 190 |
### Memory Assertions |
| 191 |
|
| 192 |
TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) |
| 193 |
|
| 194 |
Compare two blocks of memory. |
| 195 |
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. |
| 196 |
|
| 197 |
### \_MESSAGE |
| 198 |
|
| 199 |
You can append `\_MESSAGE` to any of the macros to make them take an additional argument. |
| 200 |
This argument is a string that will be printed at the end of the failure strings. |
| 201 |
This is useful for specifying more information about the problem. |
| 202 |
|
| 203 |
[CI]: https://github.com/ThrowTheSwitch/Unity/workflows/CI/badge.svg |
| 204 |
[getting started guide]: docs/UnityGettingStartedGuide.md |
| 205 |
[docs]: docs/ |
| 206 |
[UnityAssertionsReference.md]: docs/UnityAssertionsReference.md |