19750e409Sopenharmony_ci# Unity Configuration Guide
29750e409Sopenharmony_ci
39750e409Sopenharmony_ci## C Standards, Compilers and Microcontrollers
49750e409Sopenharmony_ci
59750e409Sopenharmony_ciThe embedded software world contains its challenges. Compilers support different
69750e409Sopenharmony_cirevisions of the C Standard. They ignore requirements in places, sometimes to
79750e409Sopenharmony_cimake the language more usable in some special regard. Sometimes it's to simplify
89750e409Sopenharmony_citheir support. Sometimes it's due to specific quirks of the microcontroller they
99750e409Sopenharmony_ciare targeting. Simulators add another dimension to this menagerie.
109750e409Sopenharmony_ci
119750e409Sopenharmony_ciUnity is designed to run on almost anything that is targeted by a C compiler. It
129750e409Sopenharmony_ciwould be awesome if this could be done with zero configuration. While there are
139750e409Sopenharmony_cisome targets that come close to this dream, it is sadly not universal. It is
149750e409Sopenharmony_cilikely that you are going to need at least a couple of the configuration options
159750e409Sopenharmony_cidescribed in this document.
169750e409Sopenharmony_ci
179750e409Sopenharmony_ciAll of Unity's configuration options are `#defines`. Most of these are simple
189750e409Sopenharmony_cidefinitions. A couple are macros with arguments. They live inside the
199750e409Sopenharmony_ciunity_internals.h header file. We don't necessarily recommend opening that file
209750e409Sopenharmony_ciunless you really need to. That file is proof that a cross-platform library is
219750e409Sopenharmony_cichallenging to build. From a more positive perspective, it is also proof that a
229750e409Sopenharmony_cigreat deal of complexity can be centralized primarily to one place in order to
239750e409Sopenharmony_ciprovide a more consistent and simple experience elsewhere.
249750e409Sopenharmony_ci
259750e409Sopenharmony_ci
269750e409Sopenharmony_ci### Using These Options
279750e409Sopenharmony_ci
289750e409Sopenharmony_ciIt doesn't matter if you're using a target-specific compiler and a simulator or
299750e409Sopenharmony_cia native compiler. In either case, you've got a couple choices for configuring
309750e409Sopenharmony_cithese options:
319750e409Sopenharmony_ci
329750e409Sopenharmony_ci1. Because these options are specified via C defines, you can pass most of these
339750e409Sopenharmony_cioptions to your compiler through command line compiler flags. Even if you're
349750e409Sopenharmony_ciusing an embedded target that forces you to use their overbearing IDE for all
359750e409Sopenharmony_ciconfiguration, there will be a place somewhere in your project to configure
369750e409Sopenharmony_cidefines for your compiler.
379750e409Sopenharmony_ci2. You can create a custom `unity_config.h` configuration file (present in your
389750e409Sopenharmony_citoolchain's search paths). In this file, you will list definitions and macros
399750e409Sopenharmony_cispecific to your target. All you must do is define `UNITY_INCLUDE_CONFIG_H` and
409750e409Sopenharmony_ciUnity will rely on `unity_config.h` for any further definitions it may need.
419750e409Sopenharmony_ci
429750e409Sopenharmony_ci
439750e409Sopenharmony_ci## The Options
449750e409Sopenharmony_ci
459750e409Sopenharmony_ci### Integer Types
469750e409Sopenharmony_ci
479750e409Sopenharmony_ciIf you've been a C developer for long, you probably already know that C's
489750e409Sopenharmony_ciconcept of an integer varies from target to target. The C Standard has rules
499750e409Sopenharmony_ciabout the `int` matching the register size of the target microprocessor. It has
509750e409Sopenharmony_cirules about the `int` and how its size relates to other integer types. An `int`
519750e409Sopenharmony_cion one target might be 16 bits while on another target it might be 64. There are
529750e409Sopenharmony_cimore specific types in compilers compliant with C99 or later, but that's
539750e409Sopenharmony_cicertainly not every compiler you are likely to encounter. Therefore, Unity has a
549750e409Sopenharmony_cinumber of features for helping to adjust itself to match your required integer
559750e409Sopenharmony_cisizes. It starts off by trying to do it automatically.
569750e409Sopenharmony_ci
579750e409Sopenharmony_ci
589750e409Sopenharmony_ci##### `UNITY_EXCLUDE_STDINT_H`
599750e409Sopenharmony_ci
609750e409Sopenharmony_ciThe first thing that Unity does to guess your types is check `stdint.h`.
619750e409Sopenharmony_ciThis file includes defines like `UINT_MAX` that Unity can make use of to
629750e409Sopenharmony_cilearn a lot about your system. It's possible you don't want it to do this
639750e409Sopenharmony_ci(um. why not?) or (more likely) it's possible that your system doesn't
649750e409Sopenharmony_cisupport `stdint.h`. If that's the case, you're going to want to define this.
659750e409Sopenharmony_ciThat way, Unity will know to skip the inclusion of this file and you won't
669750e409Sopenharmony_cibe left with a compiler error.
679750e409Sopenharmony_ci
689750e409Sopenharmony_ci_Example:_
699750e409Sopenharmony_ci        #define UNITY_EXCLUDE_STDINT_H
709750e409Sopenharmony_ci
719750e409Sopenharmony_ci
729750e409Sopenharmony_ci##### `UNITY_EXCLUDE_LIMITS_H`
739750e409Sopenharmony_ci
749750e409Sopenharmony_ciThe second attempt to guess your types is to check `limits.h`. Some compilers
759750e409Sopenharmony_cithat don't support `stdint.h` could include `limits.h` instead. If you don't
769750e409Sopenharmony_ciwant Unity to check this file either, define this to make it skip the inclusion.
779750e409Sopenharmony_ci
789750e409Sopenharmony_ci_Example:_
799750e409Sopenharmony_ci        #define UNITY_EXCLUDE_LIMITS_H
809750e409Sopenharmony_ci
819750e409Sopenharmony_ci
829750e409Sopenharmony_ciIf you've disabled both of the automatic options above, you're going to have to
839750e409Sopenharmony_cido the configuration yourself. Don't worry. Even this isn't too bad... there are
849750e409Sopenharmony_cijust a handful of defines that you are going to specify if you don't like the
859750e409Sopenharmony_cidefaults.
869750e409Sopenharmony_ci
879750e409Sopenharmony_ci
889750e409Sopenharmony_ci##### `UNITY_INT_WIDTH`
899750e409Sopenharmony_ci
909750e409Sopenharmony_ciDefine this to be the number of bits an `int` takes up on your system. The
919750e409Sopenharmony_cidefault, if not autodetected, is 32 bits.
929750e409Sopenharmony_ci
939750e409Sopenharmony_ci_Example:_
949750e409Sopenharmony_ci        #define UNITY_INT_WIDTH 16
959750e409Sopenharmony_ci
969750e409Sopenharmony_ci
979750e409Sopenharmony_ci##### `UNITY_LONG_WIDTH`
989750e409Sopenharmony_ci
999750e409Sopenharmony_ciDefine this to be the number of bits a `long` takes up on your system. The
1009750e409Sopenharmony_cidefault, if not autodetected, is 32 bits. This is used to figure out what kind
1019750e409Sopenharmony_ciof 64-bit support your system can handle. Does it need to specify a `long` or a
1029750e409Sopenharmony_ci`long long` to get a 64-bit value. On 16-bit systems, this option is going to be
1039750e409Sopenharmony_ciignored.
1049750e409Sopenharmony_ci
1059750e409Sopenharmony_ci_Example:_
1069750e409Sopenharmony_ci        #define UNITY_LONG_WIDTH 16
1079750e409Sopenharmony_ci
1089750e409Sopenharmony_ci
1099750e409Sopenharmony_ci##### `UNITY_POINTER_WIDTH`
1109750e409Sopenharmony_ci
1119750e409Sopenharmony_ciDefine this to be the number of bits a pointer takes up on your system. The
1129750e409Sopenharmony_cidefault, if not autodetected, is 32-bits. If you're getting ugly compiler
1139750e409Sopenharmony_ciwarnings about casting from pointers, this is the one to look at.
1149750e409Sopenharmony_ci
1159750e409Sopenharmony_ci_Example:_
1169750e409Sopenharmony_ci        #define UNITY_POINTER_WIDTH 64
1179750e409Sopenharmony_ci
1189750e409Sopenharmony_ci
1199750e409Sopenharmony_ci##### `UNITY_SUPPORT_64`
1209750e409Sopenharmony_ci
1219750e409Sopenharmony_ciUnity will automatically include 64-bit support if it auto-detects it, or if
1229750e409Sopenharmony_ciyour `int`, `long`, or pointer widths are greater than 32-bits. Define this to
1239750e409Sopenharmony_cienable 64-bit support if none of the other options already did it for you. There
1249750e409Sopenharmony_cican be a significant size and speed impact to enabling 64-bit support on small
1259750e409Sopenharmony_citargets, so don't define it if you don't need it.
1269750e409Sopenharmony_ci
1279750e409Sopenharmony_ci_Example:_
1289750e409Sopenharmony_ci        #define UNITY_SUPPORT_64
1299750e409Sopenharmony_ci
1309750e409Sopenharmony_ci
1319750e409Sopenharmony_ci### Floating Point Types
1329750e409Sopenharmony_ci
1339750e409Sopenharmony_ciIn the embedded world, it's not uncommon for targets to have no support for
1349750e409Sopenharmony_cifloating point operations at all or to have support that is limited to only
1359750e409Sopenharmony_cisingle precision. We are able to guess integer sizes on the fly because integers
1369750e409Sopenharmony_ciare always available in at least one size. Floating point, on the other hand, is
1379750e409Sopenharmony_cisometimes not available at all. Trying to include `float.h` on these platforms
1389750e409Sopenharmony_ciwould result in an error. This leaves manual configuration as the only option.
1399750e409Sopenharmony_ci
1409750e409Sopenharmony_ci
1419750e409Sopenharmony_ci##### `UNITY_INCLUDE_FLOAT`
1429750e409Sopenharmony_ci
1439750e409Sopenharmony_ci##### `UNITY_EXCLUDE_FLOAT`
1449750e409Sopenharmony_ci
1459750e409Sopenharmony_ci##### `UNITY_INCLUDE_DOUBLE`
1469750e409Sopenharmony_ci
1479750e409Sopenharmony_ci##### `UNITY_EXCLUDE_DOUBLE`
1489750e409Sopenharmony_ci
1499750e409Sopenharmony_ciBy default, Unity guesses that you will want single precision floating point
1509750e409Sopenharmony_cisupport, but not double precision. It's easy to change either of these using the
1519750e409Sopenharmony_ciinclude and exclude options here. You may include neither, either, or both, as
1529750e409Sopenharmony_cisuits your needs. For features that are enabled, the following floating point
1539750e409Sopenharmony_cioptions also become available.
1549750e409Sopenharmony_ci
1559750e409Sopenharmony_ci_Example:_
1569750e409Sopenharmony_ci
1579750e409Sopenharmony_ci        //what manner of strange processor is this?
1589750e409Sopenharmony_ci        #define UNITY_EXCLUDE_FLOAT
1599750e409Sopenharmony_ci        #define UNITY_INCLUDE_DOUBLE
1609750e409Sopenharmony_ci
1619750e409Sopenharmony_ci
1629750e409Sopenharmony_ci##### `UNITY_EXCLUDE_FLOAT_PRINT`
1639750e409Sopenharmony_ci
1649750e409Sopenharmony_ciUnity aims for as small of a footprint as possible and avoids most standard
1659750e409Sopenharmony_cilibrary calls (some embedded platforms don’t have a standard library!). Because
1669750e409Sopenharmony_ciof this, its routines for printing integer values are minimalist and hand-coded.
1679750e409Sopenharmony_ciTherefore, the display of floating point values during a failure are optional.
1689750e409Sopenharmony_ciBy default, Unity will print the actual results of floating point assertion
1699750e409Sopenharmony_cifailure (e.g. ”Expected 4.56 Was 4.68”). To not include this extra support, you
1709750e409Sopenharmony_cican use this define to instead respond to a failed assertion with a message like
1719750e409Sopenharmony_ci”Values Not Within Delta”. If you would like verbose failure messages for floating
1729750e409Sopenharmony_cipoint assertions, use these options to give more explicit failure messages.
1739750e409Sopenharmony_ci
1749750e409Sopenharmony_ci_Example:_
1759750e409Sopenharmony_ci        #define UNITY_EXCLUDE_FLOAT_PRINT
1769750e409Sopenharmony_ci
1779750e409Sopenharmony_ci
1789750e409Sopenharmony_ci##### `UNITY_FLOAT_TYPE`
1799750e409Sopenharmony_ci
1809750e409Sopenharmony_ciIf enabled, Unity assumes you want your `FLOAT` asserts to compare standard C
1819750e409Sopenharmony_cifloats. If your compiler supports a specialty floating point type, you can
1829750e409Sopenharmony_cialways override this behavior by using this definition.
1839750e409Sopenharmony_ci
1849750e409Sopenharmony_ci_Example:_
1859750e409Sopenharmony_ci        #define UNITY_FLOAT_TYPE float16_t
1869750e409Sopenharmony_ci
1879750e409Sopenharmony_ci
1889750e409Sopenharmony_ci##### `UNITY_DOUBLE_TYPE`
1899750e409Sopenharmony_ci
1909750e409Sopenharmony_ciIf enabled, Unity assumes you want your `DOUBLE` asserts to compare standard C
1919750e409Sopenharmony_cidoubles. If you would like to change this, you can specify something else by
1929750e409Sopenharmony_ciusing this option. For example, defining `UNITY_DOUBLE_TYPE` to `long double`
1939750e409Sopenharmony_cicould enable gargantuan floating point types on your 64-bit processor instead of
1949750e409Sopenharmony_cithe standard `double`.
1959750e409Sopenharmony_ci
1969750e409Sopenharmony_ci_Example:_
1979750e409Sopenharmony_ci        #define UNITY_DOUBLE_TYPE long double
1989750e409Sopenharmony_ci
1999750e409Sopenharmony_ci
2009750e409Sopenharmony_ci##### `UNITY_FLOAT_PRECISION`
2019750e409Sopenharmony_ci
2029750e409Sopenharmony_ci##### `UNITY_DOUBLE_PRECISION`
2039750e409Sopenharmony_ci
2049750e409Sopenharmony_ciIf you look up `UNITY_ASSERT_EQUAL_FLOAT` and `UNITY_ASSERT_EQUAL_DOUBLE` as
2059750e409Sopenharmony_cidocumented in the big daddy Unity Assertion Guide, you will learn that they are
2069750e409Sopenharmony_cinot really asserting that two values are equal but rather that two values are
2079750e409Sopenharmony_ci"close enough" to equal. "Close enough" is controlled by these precision
2089750e409Sopenharmony_ciconfiguration options. If you are working with 32-bit floats and/or 64-bit
2099750e409Sopenharmony_cidoubles (the normal on most processors), you should have no need to change these
2109750e409Sopenharmony_cioptions. They are both set to give you approximately 1 significant bit in either
2119750e409Sopenharmony_cidirection. The float precision is 0.00001 while the double is 10-12.
2129750e409Sopenharmony_ciFor further details on how this works, see the appendix of the Unity Assertion
2139750e409Sopenharmony_ciGuide.
2149750e409Sopenharmony_ci
2159750e409Sopenharmony_ci_Example:_
2169750e409Sopenharmony_ci        #define UNITY_FLOAT_PRECISION 0.001f
2179750e409Sopenharmony_ci
2189750e409Sopenharmony_ci
2199750e409Sopenharmony_ci### Toolset Customization
2209750e409Sopenharmony_ci
2219750e409Sopenharmony_ciIn addition to the options listed above, there are a number of other options
2229750e409Sopenharmony_ciwhich will come in handy to customize Unity's behavior for your specific
2239750e409Sopenharmony_citoolchain. It is possible that you may not need to touch any of these... but
2249750e409Sopenharmony_cicertain platforms, particularly those running in simulators, may need to jump
2259750e409Sopenharmony_cithrough extra hoops to operate properly. These macros will help in those
2269750e409Sopenharmony_cisituations.
2279750e409Sopenharmony_ci
2289750e409Sopenharmony_ci
2299750e409Sopenharmony_ci##### `UNITY_OUTPUT_CHAR(a)`
2309750e409Sopenharmony_ci
2319750e409Sopenharmony_ci##### `UNITY_OUTPUT_FLUSH()`
2329750e409Sopenharmony_ci
2339750e409Sopenharmony_ci##### `UNITY_OUTPUT_START()`
2349750e409Sopenharmony_ci
2359750e409Sopenharmony_ci##### `UNITY_OUTPUT_COMPLETE()`
2369750e409Sopenharmony_ci
2379750e409Sopenharmony_ciBy default, Unity prints its results to `stdout` as it runs. This works
2389750e409Sopenharmony_ciperfectly fine in most situations where you are using a native compiler for
2399750e409Sopenharmony_citesting. It works on some simulators as well so long as they have `stdout`
2409750e409Sopenharmony_cirouted back to the command line. There are times, however, where the simulator
2419750e409Sopenharmony_ciwill lack support for dumping results or you will want to route results
2429750e409Sopenharmony_cielsewhere for other reasons. In these cases, you should define the
2439750e409Sopenharmony_ci`UNITY_OUTPUT_CHAR` macro. This macro accepts a single character at a time (as
2449750e409Sopenharmony_cian `int`, since this is the parameter type of the standard C `putchar` function
2459750e409Sopenharmony_cimost commonly used). You may replace this with whatever function call you like.
2469750e409Sopenharmony_ci
2479750e409Sopenharmony_ci_Example:_
2489750e409Sopenharmony_ciSay you are forced to run your test suite on an embedded processor with no
2499750e409Sopenharmony_ci`stdout` option. You decide to route your test result output to a custom serial
2509750e409Sopenharmony_ci`RS232_putc()` function you wrote like thus:
2519750e409Sopenharmony_ci
2529750e409Sopenharmony_ci        #define UNITY_OUTPUT_CHAR(a) RS232_putc(a)
2539750e409Sopenharmony_ci        #define UNITY_OUTPUT_START() RS232_config(115200,1,8,0)
2549750e409Sopenharmony_ci        #define UNITY_OUTPUT_FLUSH() RS232_flush()
2559750e409Sopenharmony_ci        #define UNITY_OUTPUT_COMPLETE() RS232_close()
2569750e409Sopenharmony_ci
2579750e409Sopenharmony_ci_Note:_
2589750e409Sopenharmony_ci`UNITY_OUTPUT_FLUSH()` can be set to the standard out flush function simply by
2599750e409Sopenharmony_cispecifying `UNITY_USE_FLUSH_STDOUT`. No other defines are required. If you
2609750e409Sopenharmony_cispecify a custom flush function instead with `UNITY_OUTPUT_FLUSH` directly, it
2619750e409Sopenharmony_ciwill declare an instance of your function by default. If you want to disable
2629750e409Sopenharmony_cithis behavior, add `UNITY_OMIT_OUTPUT_FLUSH_HEADER_DECLARATION`.
2639750e409Sopenharmony_ci
2649750e409Sopenharmony_ci
2659750e409Sopenharmony_ci##### `UNITY_WEAK_ATTRIBUTE`
2669750e409Sopenharmony_ci
2679750e409Sopenharmony_ci##### `UNITY_WEAK_PRAGMA`
2689750e409Sopenharmony_ci
2699750e409Sopenharmony_ci##### `UNITY_NO_WEAK`
2709750e409Sopenharmony_ci
2719750e409Sopenharmony_ciFor some targets, Unity can make the otherwise required setUp() and tearDown()
2729750e409Sopenharmony_cifunctions optional. This is a nice convenience for test writers since setUp and
2739750e409Sopenharmony_citearDown don’t often actually do anything. If you’re using gcc or clang, this
2749750e409Sopenharmony_cioption is automatically defined for you. Other compilers can also support this
2759750e409Sopenharmony_cibehavior, if they support a C feature called weak functions. A weak function is
2769750e409Sopenharmony_cia function that is compiled into your executable unless a non-weak version of
2779750e409Sopenharmony_cithe same function is defined elsewhere. If a non-weak version is found, the weak
2789750e409Sopenharmony_civersion is ignored as if it never existed. If your compiler supports this feature,
2799750e409Sopenharmony_ciyou can let Unity know by defining UNITY_WEAK_ATTRIBUTE or UNITY_WEAK_PRAGMA as
2809750e409Sopenharmony_cithe function attributes that would need to be applied to identify a function as
2819750e409Sopenharmony_ciweak. If your compiler lacks support for weak functions, you will always need to
2829750e409Sopenharmony_cidefine setUp and tearDown functions (though they can be and often will be just
2839750e409Sopenharmony_ciempty). You can also force Unity to NOT use weak functions by defining
2849750e409Sopenharmony_ciUNITY_NO_WEAK. The most common options for this feature are:
2859750e409Sopenharmony_ci
2869750e409Sopenharmony_ci_Example:_
2879750e409Sopenharmony_ci        #define UNITY_WEAK_ATTRIBUTE weak
2889750e409Sopenharmony_ci        #define UNITY_WEAK_ATTRIBUTE __attribute__((weak))
2899750e409Sopenharmony_ci        #define UNITY_WEAK_PRAGMA
2909750e409Sopenharmony_ci        #define UNITY_NO_WEAK
2919750e409Sopenharmony_ci
2929750e409Sopenharmony_ci
2939750e409Sopenharmony_ci##### `UNITY_PTR_ATTRIBUTE`
2949750e409Sopenharmony_ci
2959750e409Sopenharmony_ciSome compilers require a custom attribute to be assigned to pointers, like
2969750e409Sopenharmony_ci`near` or `far`. In these cases, you can give Unity a safe default for these by
2979750e409Sopenharmony_cidefining this option with the attribute you would like.
2989750e409Sopenharmony_ci
2999750e409Sopenharmony_ci_Example:_
3009750e409Sopenharmony_ci        #define UNITY_PTR_ATTRIBUTE __attribute__((far))
3019750e409Sopenharmony_ci        #define UNITY_PTR_ATTRIBUTE near
3029750e409Sopenharmony_ci
3039750e409Sopenharmony_ci
3049750e409Sopenharmony_ci##### `UNITY_PRINT_EOL`
3059750e409Sopenharmony_ci
3069750e409Sopenharmony_ciBy default, Unity outputs \n at the end of each line of output. This is easy
3079750e409Sopenharmony_cito parse by the scripts, by Ceedling, etc, but it might not be ideal for YOUR
3089750e409Sopenharmony_cisystem. Feel free to override this and to make it whatever you wish.
3099750e409Sopenharmony_ci
3109750e409Sopenharmony_ci_Example:_
3119750e409Sopenharmony_ci        #define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\r'); UNITY_OUTPUT_CHAR('\n') }
3129750e409Sopenharmony_ci
3139750e409Sopenharmony_ci
3149750e409Sopenharmony_ci
3159750e409Sopenharmony_ci##### `UNITY_EXCLUDE_DETAILS`
3169750e409Sopenharmony_ci
3179750e409Sopenharmony_ciThis is an option for if you absolutely must squeeze every byte of memory out of
3189750e409Sopenharmony_ciyour system. Unity stores a set of internal scratchpads which are used to pass
3199750e409Sopenharmony_ciextra detail information around. It's used by systems like CMock in order to
3209750e409Sopenharmony_cireport which function or argument flagged an error. If you're not using CMock and
3219750e409Sopenharmony_ciyou're not using these details for other things, then you can exclude them.
3229750e409Sopenharmony_ci
3239750e409Sopenharmony_ci_Example:_
3249750e409Sopenharmony_ci        #define UNITY_EXCLUDE_DETAILS
3259750e409Sopenharmony_ci
3269750e409Sopenharmony_ci
3279750e409Sopenharmony_ci
3289750e409Sopenharmony_ci##### `UNITY_EXCLUDE_SETJMP`
3299750e409Sopenharmony_ci
3309750e409Sopenharmony_ciIf your embedded system doesn't support the standard library setjmp, you can
3319750e409Sopenharmony_ciexclude Unity's reliance on this by using this define. This dropped dependence
3329750e409Sopenharmony_cicomes at a price, though. You will be unable to use custom helper functions for
3339750e409Sopenharmony_ciyour tests, and you will be unable to use tools like CMock. Very likely, if your
3349750e409Sopenharmony_cicompiler doesn't support setjmp, you wouldn't have had the memory space for those
3359750e409Sopenharmony_cithings anyway, though... so this option exists for those situations.
3369750e409Sopenharmony_ci
3379750e409Sopenharmony_ci_Example:_
3389750e409Sopenharmony_ci        #define UNITY_EXCLUDE_SETJMP
3399750e409Sopenharmony_ci
3409750e409Sopenharmony_ci##### `UNITY_OUTPUT_COLOR`
3419750e409Sopenharmony_ci
3429750e409Sopenharmony_ciIf you want to add color using ANSI escape codes you can use this define.
3439750e409Sopenharmony_cit
3449750e409Sopenharmony_ci_Example:_
3459750e409Sopenharmony_ci        #define UNITY_OUTPUT_COLOR
3469750e409Sopenharmony_ci
3479750e409Sopenharmony_ci
3489750e409Sopenharmony_ci
3499750e409Sopenharmony_ci## Getting Into The Guts
3509750e409Sopenharmony_ci
3519750e409Sopenharmony_ciThere will be cases where the options above aren't quite going to get everything
3529750e409Sopenharmony_ciperfect. They are likely sufficient for any situation where you are compiling
3539750e409Sopenharmony_ciand executing your tests with a native toolchain (e.g. clang on Mac). These
3549750e409Sopenharmony_cioptions may even get you through the majority of cases encountered in working
3559750e409Sopenharmony_ciwith a target simulator run from your local command line. But especially if you
3569750e409Sopenharmony_cimust run your test suite on your target hardware, your Unity configuration will
3579750e409Sopenharmony_cirequire special help. This special help will usually reside in one of two
3589750e409Sopenharmony_ciplaces: the `main()` function or the `RUN_TEST` macro. Let's look at how these
3599750e409Sopenharmony_ciwork.
3609750e409Sopenharmony_ci
3619750e409Sopenharmony_ci
3629750e409Sopenharmony_ci##### `main()`
3639750e409Sopenharmony_ci
3649750e409Sopenharmony_ciEach test module is compiled and run on its own, separate from the other test
3659750e409Sopenharmony_cifiles in your project. Each test file, therefore, has a `main` function. This
3669750e409Sopenharmony_ci`main` function will need to contain whatever code is necessary to initialize
3679750e409Sopenharmony_ciyour system to a workable state. This is particularly true for situations where
3689750e409Sopenharmony_ciyou must set up a memory map or initialize a communication channel for the
3699750e409Sopenharmony_cioutput of your test results.
3709750e409Sopenharmony_ci
3719750e409Sopenharmony_ciA simple main function looks something like this:
3729750e409Sopenharmony_ci
3739750e409Sopenharmony_ci        int main(void) {
3749750e409Sopenharmony_ci            UNITY_BEGIN();
3759750e409Sopenharmony_ci            RUN_TEST(test_TheFirst);
3769750e409Sopenharmony_ci            RUN_TEST(test_TheSecond);
3779750e409Sopenharmony_ci            RUN_TEST(test_TheThird);
3789750e409Sopenharmony_ci            return UNITY_END();
3799750e409Sopenharmony_ci        }
3809750e409Sopenharmony_ci
3819750e409Sopenharmony_ciYou can see that our main function doesn't bother taking any arguments. For our
3829750e409Sopenharmony_cimost barebones case, we'll never have arguments because we just run all the
3839750e409Sopenharmony_citests each time. Instead, we start by calling `UNITY_BEGIN`. We run each test
3849750e409Sopenharmony_ci(in whatever order we wish). Finally, we call `UNITY_END`, returning its return
3859750e409Sopenharmony_civalue (which is the total number of failures).
3869750e409Sopenharmony_ci
3879750e409Sopenharmony_ciIt should be easy to see that you can add code before any test cases are run or
3889750e409Sopenharmony_ciafter all the test cases have completed. This allows you to do any needed
3899750e409Sopenharmony_cisystem-wide setup or teardown that might be required for your special
3909750e409Sopenharmony_cicircumstances.
3919750e409Sopenharmony_ci
3929750e409Sopenharmony_ci
3939750e409Sopenharmony_ci##### `RUN_TEST`
3949750e409Sopenharmony_ci
3959750e409Sopenharmony_ciThe `RUN_TEST` macro is called with each test case function. Its job is to
3969750e409Sopenharmony_ciperform whatever setup and teardown is necessary for executing a single test
3979750e409Sopenharmony_cicase function. This includes catching failures, calling the test module's
3989750e409Sopenharmony_ci`setUp()` and `tearDown()` functions, and calling `UnityConcludeTest()`. If
3999750e409Sopenharmony_ciusing CMock or test coverage, there will be additional stubs in use here. A
4009750e409Sopenharmony_cisimple minimalist RUN_TEST macro looks something like this:
4019750e409Sopenharmony_ci
4029750e409Sopenharmony_ci        #define RUN_TEST(testfunc) \
4039750e409Sopenharmony_ci            UNITY_NEW_TEST(#testfunc) \
4049750e409Sopenharmony_ci            if (TEST_PROTECT()) { \
4059750e409Sopenharmony_ci                setUp(); \
4069750e409Sopenharmony_ci                testfunc(); \
4079750e409Sopenharmony_ci            } \
4089750e409Sopenharmony_ci            if (TEST_PROTECT() && (!TEST_IS_IGNORED)) \
4099750e409Sopenharmony_ci                tearDown(); \
4109750e409Sopenharmony_ci            UnityConcludeTest();
4119750e409Sopenharmony_ci
4129750e409Sopenharmony_ciSo that's quite a macro, huh? It gives you a glimpse of what kind of stuff Unity
4139750e409Sopenharmony_cihas to deal with for every single test case. For each test case, we declare that
4149750e409Sopenharmony_ciit is a new test. Then we run `setUp` and our test function. These are run
4159750e409Sopenharmony_ciwithin a `TEST_PROTECT` block, the function of which is to handle failures that
4169750e409Sopenharmony_cioccur during the test. Then, assuming our test is still running and hasn't been
4179750e409Sopenharmony_ciignored, we run `tearDown`. No matter what, our last step is to conclude this
4189750e409Sopenharmony_citest before moving on to the next.
4199750e409Sopenharmony_ci
4209750e409Sopenharmony_ciLet's say you need to add a call to `fsync` to force all of your output data to
4219750e409Sopenharmony_ciflush to a file after each test. You could easily insert this after your
4229750e409Sopenharmony_ci`UnityConcludeTest` call. Maybe you want to write an xml tag before and after
4239750e409Sopenharmony_cieach result set. Again, you could do this by adding lines to this macro. Updates
4249750e409Sopenharmony_cito this macro are for the occasions when you need an action before or after
4259750e409Sopenharmony_cievery single test case throughout your entire suite of tests.
4269750e409Sopenharmony_ci
4279750e409Sopenharmony_ci
4289750e409Sopenharmony_ci## Happy Porting
4299750e409Sopenharmony_ci
4309750e409Sopenharmony_ciThe defines and macros in this guide should help you port Unity to just about
4319750e409Sopenharmony_ciany C target we can imagine. If you run into a snag or two, don't be afraid of
4329750e409Sopenharmony_ciasking for help on the forums. We love a good challenge!
4339750e409Sopenharmony_ci
4349750e409Sopenharmony_ci
4359750e409Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*
436