19750e409Sopenharmony_ciUnity Test API 29750e409Sopenharmony_ci============== 39750e409Sopenharmony_ci 49750e409Sopenharmony_ci[](https://travis-ci.org/ThrowTheSwitch/Unity) 59750e409Sopenharmony_ci__Copyright (c) 2007 - 2017 Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams__ 69750e409Sopenharmony_ci 79750e409Sopenharmony_ciRunning Tests 89750e409Sopenharmony_ci------------- 99750e409Sopenharmony_ci 109750e409Sopenharmony_ci RUN_TEST(func, linenum) 119750e409Sopenharmony_ci 129750e409Sopenharmony_ciEach 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. 139750e409Sopenharmony_ci 149750e409Sopenharmony_ciIgnoring Tests 159750e409Sopenharmony_ci-------------- 169750e409Sopenharmony_ci 179750e409Sopenharmony_ciThere 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. 189750e409Sopenharmony_ci 199750e409Sopenharmony_ci TEST_IGNORE() 209750e409Sopenharmony_ci 219750e409Sopenharmony_ciIgnore this test and return immediately 229750e409Sopenharmony_ci 239750e409Sopenharmony_ci TEST_IGNORE_MESSAGE (message) 249750e409Sopenharmony_ci 259750e409Sopenharmony_ciIgnore this test and return immediately. Output a message stating why the test was ignored. 269750e409Sopenharmony_ci 279750e409Sopenharmony_ciAborting Tests 289750e409Sopenharmony_ci-------------- 299750e409Sopenharmony_ci 309750e409Sopenharmony_ciThere 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. 319750e409Sopenharmony_ci 329750e409Sopenharmony_ci TEST_PROTECT() 339750e409Sopenharmony_ci 349750e409Sopenharmony_ciSetup and Catch macro 359750e409Sopenharmony_ci 369750e409Sopenharmony_ci TEST_ABORT() 379750e409Sopenharmony_ci 389750e409Sopenharmony_ciAbort Test macro 399750e409Sopenharmony_ci 409750e409Sopenharmony_ciExample: 419750e409Sopenharmony_ci 429750e409Sopenharmony_ci main() 439750e409Sopenharmony_ci { 449750e409Sopenharmony_ci if (TEST_PROTECT()) 459750e409Sopenharmony_ci { 469750e409Sopenharmony_ci MyTest(); 479750e409Sopenharmony_ci } 489750e409Sopenharmony_ci } 499750e409Sopenharmony_ci 509750e409Sopenharmony_ciIf MyTest calls `TEST_ABORT`, program control will immediately return to `TEST_PROTECT` with a return value of zero. 519750e409Sopenharmony_ci 529750e409Sopenharmony_ci 539750e409Sopenharmony_ciUnity Assertion Summary 549750e409Sopenharmony_ci======================= 559750e409Sopenharmony_ci 569750e409Sopenharmony_ciBasic Validity Tests 579750e409Sopenharmony_ci-------------------- 589750e409Sopenharmony_ci 599750e409Sopenharmony_ci TEST_ASSERT_TRUE(condition) 609750e409Sopenharmony_ci 619750e409Sopenharmony_ciEvaluates whatever code is in condition and fails if it evaluates to false 629750e409Sopenharmony_ci 639750e409Sopenharmony_ci TEST_ASSERT_FALSE(condition) 649750e409Sopenharmony_ci 659750e409Sopenharmony_ciEvaluates whatever code is in condition and fails if it evaluates to true 669750e409Sopenharmony_ci 679750e409Sopenharmony_ci TEST_ASSERT(condition) 689750e409Sopenharmony_ci 699750e409Sopenharmony_ciAnother way of calling `TEST_ASSERT_TRUE` 709750e409Sopenharmony_ci 719750e409Sopenharmony_ci TEST_ASSERT_UNLESS(condition) 729750e409Sopenharmony_ci 739750e409Sopenharmony_ciAnother way of calling `TEST_ASSERT_FALSE` 749750e409Sopenharmony_ci 759750e409Sopenharmony_ci TEST_FAIL() 769750e409Sopenharmony_ci TEST_FAIL_MESSAGE(message) 779750e409Sopenharmony_ci 789750e409Sopenharmony_ciThis test is automatically marked as a failure. The message is output stating why. 799750e409Sopenharmony_ci 809750e409Sopenharmony_ciNumerical Assertions: Integers 819750e409Sopenharmony_ci------------------------------ 829750e409Sopenharmony_ci 839750e409Sopenharmony_ci TEST_ASSERT_EQUAL_INT(expected, actual) 849750e409Sopenharmony_ci TEST_ASSERT_EQUAL_INT8(expected, actual) 859750e409Sopenharmony_ci TEST_ASSERT_EQUAL_INT16(expected, actual) 869750e409Sopenharmony_ci TEST_ASSERT_EQUAL_INT32(expected, actual) 879750e409Sopenharmony_ci TEST_ASSERT_EQUAL_INT64(expected, actual) 889750e409Sopenharmony_ci 899750e409Sopenharmony_ciCompare two integers for equality and display errors as signed integers. A cast will be performed 909750e409Sopenharmony_cito your natural integer size so often this can just be used. When you need to specify the exact size, 919750e409Sopenharmony_cilike when comparing arrays, you can use a specific version: 929750e409Sopenharmony_ci 939750e409Sopenharmony_ci TEST_ASSERT_EQUAL_UINT(expected, actual) 949750e409Sopenharmony_ci TEST_ASSERT_EQUAL_UINT8(expected, actual) 959750e409Sopenharmony_ci TEST_ASSERT_EQUAL_UINT16(expected, actual) 969750e409Sopenharmony_ci TEST_ASSERT_EQUAL_UINT32(expected, actual) 979750e409Sopenharmony_ci TEST_ASSERT_EQUAL_UINT64(expected, actual) 989750e409Sopenharmony_ci 999750e409Sopenharmony_ciCompare two integers for equality and display errors as unsigned integers. Like INT, there are 1009750e409Sopenharmony_civariants for different sizes also. 1019750e409Sopenharmony_ci 1029750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX(expected, actual) 1039750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX8(expected, actual) 1049750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX16(expected, actual) 1059750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX32(expected, actual) 1069750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX64(expected, actual) 1079750e409Sopenharmony_ci 1089750e409Sopenharmony_ciCompares two integers for equality and display errors as hexadecimal. Like the other integer comparisons, 1099750e409Sopenharmony_ciyou can specify the size... here the size will also effect how many nibbles are shown (for example, `HEX16` 1109750e409Sopenharmony_ciwill show 4 nibbles). 1119750e409Sopenharmony_ci 1129750e409Sopenharmony_ci TEST_ASSERT_EQUAL(expected, actual) 1139750e409Sopenharmony_ci 1149750e409Sopenharmony_ciAnother way of calling TEST_ASSERT_EQUAL_INT 1159750e409Sopenharmony_ci 1169750e409Sopenharmony_ci TEST_ASSERT_INT_WITHIN(delta, expected, actual) 1179750e409Sopenharmony_ci 1189750e409Sopenharmony_ciAsserts that the actual value is within plus or minus delta of the expected value. This also comes in 1199750e409Sopenharmony_cisize specific variants. 1209750e409Sopenharmony_ci 1219750e409Sopenharmony_ci 1229750e409Sopenharmony_ci TEST_ASSERT_GREATER_THAN(threshold, actual) 1239750e409Sopenharmony_ci 1249750e409Sopenharmony_ciAsserts that the actual value is greater than the threshold. This also comes in size specific variants. 1259750e409Sopenharmony_ci 1269750e409Sopenharmony_ci 1279750e409Sopenharmony_ci TEST_ASSERT_LESS_THAN(threshold, actual) 1289750e409Sopenharmony_ci 1299750e409Sopenharmony_ciAsserts that the actual value is less than the threshold. This also comes in size specific variants. 1309750e409Sopenharmony_ci 1319750e409Sopenharmony_ci 1329750e409Sopenharmony_ciArrays 1339750e409Sopenharmony_ci------ 1349750e409Sopenharmony_ci 1359750e409Sopenharmony_ci _ARRAY 1369750e409Sopenharmony_ci 1379750e409Sopenharmony_ciYou can append `_ARRAY` to any of these macros to make an array comparison of that type. Here you will 1389750e409Sopenharmony_cineed to care a bit more about the actual size of the value being checked. You will also specify an 1399750e409Sopenharmony_ciadditional argument which is the number of elements to compare. For example: 1409750e409Sopenharmony_ci 1419750e409Sopenharmony_ci TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, elements) 1429750e409Sopenharmony_ci 1439750e409Sopenharmony_ci _EACH_EQUAL 1449750e409Sopenharmony_ci 1459750e409Sopenharmony_ciAnother array comparison option is to check that EVERY element of an array is equal to a single expected 1469750e409Sopenharmony_civalue. You do this by specifying the EACH_EQUAL macro. For example: 1479750e409Sopenharmony_ci 1489750e409Sopenharmony_ci TEST_ASSERT_EACH_EQUAL_INT32(expected, actual, elements) 1499750e409Sopenharmony_ci 1509750e409Sopenharmony_ciNumerical Assertions: Bitwise 1519750e409Sopenharmony_ci----------------------------- 1529750e409Sopenharmony_ci 1539750e409Sopenharmony_ci TEST_ASSERT_BITS(mask, expected, actual) 1549750e409Sopenharmony_ci 1559750e409Sopenharmony_ciUse an integer mask to specify which bits should be compared between two other integers. High bits in the mask are compared, low bits ignored. 1569750e409Sopenharmony_ci 1579750e409Sopenharmony_ci TEST_ASSERT_BITS_HIGH(mask, actual) 1589750e409Sopenharmony_ci 1599750e409Sopenharmony_ciUse 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. 1609750e409Sopenharmony_ci 1619750e409Sopenharmony_ci TEST_ASSERT_BITS_LOW(mask, actual) 1629750e409Sopenharmony_ci 1639750e409Sopenharmony_ciUse 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. 1649750e409Sopenharmony_ci 1659750e409Sopenharmony_ci TEST_ASSERT_BIT_HIGH(bit, actual) 1669750e409Sopenharmony_ci 1679750e409Sopenharmony_ciTest a single bit and verify that it is high. The bit is specified 0-31 for a 32-bit integer. 1689750e409Sopenharmony_ci 1699750e409Sopenharmony_ci TEST_ASSERT_BIT_LOW(bit, actual) 1709750e409Sopenharmony_ci 1719750e409Sopenharmony_ciTest a single bit and verify that it is low. The bit is specified 0-31 for a 32-bit integer. 1729750e409Sopenharmony_ci 1739750e409Sopenharmony_ciNumerical Assertions: Floats 1749750e409Sopenharmony_ci---------------------------- 1759750e409Sopenharmony_ci 1769750e409Sopenharmony_ci TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) 1779750e409Sopenharmony_ci 1789750e409Sopenharmony_ciAsserts that the actual value is within plus or minus delta of the expected value. 1799750e409Sopenharmony_ci 1809750e409Sopenharmony_ci TEST_ASSERT_EQUAL_FLOAT(expected, actual) 1819750e409Sopenharmony_ci TEST_ASSERT_EQUAL_DOUBLE(expected, actual) 1829750e409Sopenharmony_ci 1839750e409Sopenharmony_ciAsserts that two floating point values are "equal" within a small % delta of the expected value. 1849750e409Sopenharmony_ci 1859750e409Sopenharmony_ciString Assertions 1869750e409Sopenharmony_ci----------------- 1879750e409Sopenharmony_ci 1889750e409Sopenharmony_ci TEST_ASSERT_EQUAL_STRING(expected, actual) 1899750e409Sopenharmony_ci 1909750e409Sopenharmony_ciCompare two null-terminate strings. Fail if any character is different or if the lengths are different. 1919750e409Sopenharmony_ci 1929750e409Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) 1939750e409Sopenharmony_ci 1949750e409Sopenharmony_ciCompare two strings. Fail if any character is different, stop comparing after len characters. 1959750e409Sopenharmony_ci 1969750e409Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) 1979750e409Sopenharmony_ci 1989750e409Sopenharmony_ciCompare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure. 1999750e409Sopenharmony_ci 2009750e409Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) 2019750e409Sopenharmony_ci 2029750e409Sopenharmony_ciCompare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure. 2039750e409Sopenharmony_ci 2049750e409Sopenharmony_ciPointer Assertions 2059750e409Sopenharmony_ci------------------ 2069750e409Sopenharmony_ci 2079750e409Sopenharmony_ciMost pointer operations can be performed by simply using the integer comparisons above. However, a couple of special cases are added for clarity. 2089750e409Sopenharmony_ci 2099750e409Sopenharmony_ci TEST_ASSERT_NULL(pointer) 2109750e409Sopenharmony_ci 2119750e409Sopenharmony_ciFails if the pointer is not equal to NULL 2129750e409Sopenharmony_ci 2139750e409Sopenharmony_ci TEST_ASSERT_NOT_NULL(pointer) 2149750e409Sopenharmony_ci 2159750e409Sopenharmony_ciFails if the pointer is equal to NULL 2169750e409Sopenharmony_ci 2179750e409Sopenharmony_ciMemory Assertions 2189750e409Sopenharmony_ci----------------- 2199750e409Sopenharmony_ci 2209750e409Sopenharmony_ci TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) 2219750e409Sopenharmony_ci 2229750e409Sopenharmony_ciCompare two blocks of memory. This is a good generic assertion for types that can't be coerced into acting like 2239750e409Sopenharmony_cistandard types... but since it's a memory compare, you have to be careful that your data types are packed. 2249750e409Sopenharmony_ci 2259750e409Sopenharmony_ci_MESSAGE 2269750e409Sopenharmony_ci-------- 2279750e409Sopenharmony_ci 2289750e409Sopenharmony_ciyou can append _MESSAGE to any of the macros to make them take an additional argument. This argument 2299750e409Sopenharmony_ciis a string that will be printed at the end of the failure strings. This is useful for specifying more 2309750e409Sopenharmony_ciinformation about the problem. 2319750e409Sopenharmony_ci 232