• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Unity - Getting Started
2
3## Welcome
4
5Congratulations. You're now the proud owner of your very own pile of bits! What
6are you going to do with all these ones and zeros? This document should be able
7to help you decide just that.
8
9Unity is a unit test framework. The goal has been to keep it small and
10functional. The core Unity test framework is three files: a single C file and a
11couple header files. These team up to provide functions and macros to make
12testing easier.
13
14Unity was designed to be cross-platform. It works hard to stick with C standards
15while still providing support for the many embedded C compilers that bend the
16rules. Unity has been used with many compilers, including GCC, IAR, Clang,
17Green Hills, Microchip, and MS Visual Studio. It's not much work to get it to
18work with a new target.
19
20
21### Overview of the Documents
22
23#### Unity Assertions reference
24
25This document will guide you through all the assertion options provided by
26Unity. This is going to be your unit testing bread and butter. You'll spend more
27time with assertions than any other part of Unity.
28
29
30#### Unity Assertions Cheat Sheet
31
32This document contains an abridged summary of the assertions described in the
33previous document. It's perfect for printing and referencing while you
34familiarize yourself with Unity's options.
35
36
37#### Unity Configuration Guide
38
39This document is the one to reference when you are going to use Unity with a new
40target or compiler. It'll guide you through the configuration options and will
41help you customize your testing experience to meet your needs.
42
43
44#### Unity Helper Scripts
45
46This document describes the helper scripts that are available for simplifying
47your testing workflow. It describes the collection of optional Ruby scripts
48included in the auto directory of your Unity installation. Neither Ruby nor
49these scripts are necessary for using Unity. They are provided as a convenience
50for those who wish to use them.
51
52
53#### Unity License
54
55What's an open source project without a license file? This brief document
56describes the terms you're agreeing to when you use this software. Basically, we
57want it to be useful to you in whatever context you want to use it, but please
58don't blame us if you run into problems.
59
60
61### Overview of the Folders
62
63If you have obtained Unity through Github or something similar, you might be
64surprised by just how much stuff you suddenly have staring you in the face.
65Don't worry, Unity itself is very small. The rest of it is just there to make
66your life easier. You can ignore it or use it at your convenience. Here's an
67overview of everything in the project.
68
69- `src` - This is the code you care about! This folder contains a C file and two
70header files. These three files _are_ Unity.
71- `docs` - You're reading this document, so it's possible you have found your way
72into this folder already. This is where all the handy documentation can be
73found.
74- `examples` - This contains a few examples of using Unity.
75- `extras` - These are optional add ons to Unity that are not part of the core
76project. If you've reached us through James Grenning's book, you're going to
77want to look here.
78- `test` - This is how Unity and its scripts are all tested. If you're just using
79Unity, you'll likely never need to go in here. If you are the lucky team member
80who gets to port Unity to a new toolchain, this is a good place to verify
81everything is configured properly.
82- `auto` - Here you will find helpful Ruby scripts for simplifying your test
83workflow. They are purely optional and are not required to make use of Unity.
84
85
86## How to Create A Test File
87
88Test files are C files. Most often you will create a single test file for each C
89module that you want to test. The test file should include unity.h and the
90header for your C module to be tested.
91
92Next, a test file will include a `setUp()` and `tearDown()` function. The setUp
93function can contain anything you would like to run before each test. The
94tearDown function can contain anything you would like to run after each test.
95Both functions accept no arguments and return nothing. You may leave either or
96both of these blank if you have no need for them.
97
98If you're using Ceedling or the test runner generator script, you may leave these off
99completely. Not sure? Give it a try. If you compiler complains that it can't
100find setUp or tearDown when it links, you'll know you need to at least include
101an empty function for these.
102
103The majority of the file will be a series of test functions. Test functions
104follow the convention of starting with the word "test_" or "spec_". You don't HAVE
105to name them this way, but it makes it clear what functions are tests for other
106developers.  Also, the automated scripts that come with Unity or Ceedling will default
107to looking for test functions to be prefixed this way. Test functions take no arguments
108and return nothing. All test accounting is handled internally in Unity.
109
110Finally, at the bottom of your test file, you will write a `main()` function.
111This function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and
112finally `UNITY_END()`.This is what will actually trigger each of those test
113functions to run, so it is important that each function gets its own `RUN_TEST`
114call.
115
116Remembering to add each test to the main function can get to be tedious. If you
117enjoy using helper scripts in your build process, you might consider making use
118of our handy [generate_test_runner.rb](../auto/generate_test_runner.rb) script.
119This will create the main function and all the calls for you, assuming that you
120have followed the suggested naming conventions. In this case, there is no need
121for you to include the main function in your test file at all.
122
123When you're done, your test file will look something like this:
124
125```C
126#include "unity.h"
127#include "file_to_test.h"
128
129void setUp(void) {
130    // set stuff up here
131}
132
133void tearDown(void) {
134    // clean stuff up here
135}
136
137void test_function_should_doBlahAndBlah(void) {
138    //test stuff
139}
140
141void test_function_should_doAlsoDoBlah(void) {
142    //more test stuff
143}
144
145// not needed when using generate_test_runner.rb
146int main(void) {
147    UNITY_BEGIN();
148    RUN_TEST(test_function_should_doBlahAndBlah);
149    RUN_TEST(test_function_should_doAlsoDoBlah);
150    return UNITY_END();
151}
152```
153
154It's possible that you will need more customization than this, eventually.
155For that sort of thing, you're going to want to look at the configuration guide.
156This should be enough to get you going, though.
157
158### Running Test Functions
159When writing your own `main()` functions, for a test-runner. There are two ways
160to execute the test.
161
162The classic variant
163``` c
164RUN_TEST(func, linenum)
165```
166or its simpler replacement that starts at the beginning of the function.
167``` c
168RUN_TEST(func)
169```
170These macros perform the necessary setup before the test is called and
171handles cleanup and result tabulation afterwards.
172
173### Ignoring Test Functions
174There are times when a test is incomplete or not valid for some reason.
175At these times, TEST_IGNORE can be called. Control will immediately be
176returned to the caller of the test, and no failures will be returned.
177This is useful when your test runners are automatically generated.
178
179``` c
180TEST_IGNORE()
181```
182
183Ignore this test and return immediately
184
185``` c
186TEST_IGNORE_MESSAGE (message)
187```
188
189Ignore this test and return immediately. Output a message stating why the test was ignored.
190
191### Aborting Tests
192There 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.
193
194    TEST_PROTECT()
195
196Setup and Catch macro
197
198    TEST_ABORT()
199
200Abort Test macro
201
202Example:
203
204    main()
205    {
206        if (TEST_PROTECT())
207        {
208            MyTest();
209        }
210    }
211
212If MyTest calls `TEST_ABORT`, program control will immediately return to `TEST_PROTECT` with a return value of zero.
213
214
215
216## How to Build and Run A Test File
217
218This is the single biggest challenge to picking up a new unit testing framework,
219at least in a language like C or C++. These languages are REALLY good at getting
220you "close to the metal" (why is the phrase metal? Wouldn't it be more accurate
221to say "close to the silicon"?). While this feature is usually a good thing, it
222can make testing more challenging.
223
224You have two really good options for toolchains. Depending on where you're
225coming from, it might surprise you that neither of these options is running the
226unit tests on your hardware.
227There are many reasons for this, but here's a short version:
228- On hardware, you have too many constraints (processing power, memory, etc),
229- On hardware, you don't have complete control over all registers,
230- On hardware, unit testing is more challenging,
231- Unit testing isn't System testing. Keep them separate.
232
233Instead of running your tests on your actual hardware, most developers choose to
234develop them as native applications (using gcc or MSVC for example) or as
235applications running on a simulator. Either is a good option. Native apps have
236the advantages of being faster and easier to set up. Simulator apps have the
237advantage of working with the same compiler as your target application. The
238options for configuring these are discussed in the configuration guide.
239
240To get either to work, you might need to make a few changes to the file
241containing your register set (discussed later).
242
243In either case, a test is built by linking unity, the test file, and the C
244file(s) being tested. These files create an executable which can be run as the
245test set for that module. Then, this process is repeated for the next test file.
246This flexibility of separating tests into individual executables allows us to
247much more thoroughly unit test our system and it keeps all the test code out of
248our final release!
249
250
251*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*
252