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