UnityGettingStartedGuide.md 9.9 KB
Newer Older
T
toby 已提交
1 2 3
# Unity - Getting Started

## Welcome
4 5 6

Congratulations. You're now the proud owner of your very own pile of bits! What
are you going to do with all these ones and zeros? This document should be able
T
toby 已提交
7 8
to help you decide just that.

9 10 11
Unity is a unit test framework. The goal has been to keep it small and
functional. The core Unity test framework is three files: a single C file and a
couple header files. These team up to provide functions and macros to make
T
toby 已提交
12 13
testing easier.

D
Deryew 已提交
14
Unity was designed to be cross-platform. It works hard to stick with C standards
15 16 17
while still providing support for the many embedded C compilers that bend the
rules. Unity has been used with many compilers, including GCC, IAR, Clang,
Green Hills, Microchip, and MS Visual Studio. It's not much work to get it to
T
toby 已提交
18 19
work with a new target.

20

T
toby 已提交
21 22 23
### Overview of the Documents

#### Unity Assertions reference
24 25 26

This document will guide you through all the assertion options provided by
Unity. This is going to be your unit testing bread and butter. You'll spend more
T
toby 已提交
27 28
time with assertions than any other part of Unity.

29

T
toby 已提交
30
#### Unity Assertions Cheat Sheet
31 32 33

This document contains an abridged summary of the assertions described in the
previous document. It's perfect for printing and referencing while you
T
toby 已提交
34 35
familiarize yourself with Unity's options.

36

T
toby 已提交
37
#### Unity Configuration Guide
38 39 40

This document is the one to reference when you are going to use Unity with a new
target or compiler. It'll guide you through the configuration options and will
T
toby 已提交
41 42
help you customize your testing experience to meet your needs.

43

T
toby 已提交
44
#### Unity Helper Scripts
45 46 47 48 49

This document describes the helper scripts that are available for simplifying
your testing workflow. It describes the collection of optional Ruby scripts
included in the auto directory of your Unity installation. Neither Ruby nor
these scripts are necessary for using Unity. They are provided as a convenience
T
toby 已提交
50 51
for those who wish to use them.

52

T
toby 已提交
53
#### Unity License
54 55 56 57

What's an open source project without a license file? This brief document
describes the terms you're agreeing to when you use this software. Basically, we
want it to be useful to you in whatever context you want to use it, but please
T
toby 已提交
58 59
don't blame us if you run into problems.

60

T
toby 已提交
61
### Overview of the Folders
62 63

If you have obtained Unity through Github or something similar, you might be
T
toby 已提交
64
surprised by just how much stuff you suddenly have staring you in the face.
65 66
Don't worry, Unity itself is very small. The rest of it is just there to make
your life easier. You can ignore it or use it at your convenience. Here's an
T
toby 已提交
67 68
overview of everything in the project.

69
- `src` - This is the code you care about! This folder contains a C file and two
T
toby 已提交
70
header files. These three files _are_ Unity.
71 72
- `docs` - You're reading this document, so it's possible you have found your way
into this folder already. This is where all the handy documentation can be
T
toby 已提交
73
found.
74 75 76
- `examples` - This contains a few examples of using Unity.
- `extras` - These are optional add ons to Unity that are not part of the core
project. If you've reached us through James Grenning's book, you're going to
T
toby 已提交
77
want to look here.
78 79 80
- `test` - This is how Unity and its scripts are all tested. If you're just using
Unity, you'll likely never need to go in here. If you are the lucky team member
who gets to port Unity to a new toolchain, this is a good place to verify
T
toby 已提交
81
everything is configured properly.
82
- `auto` - Here you will find helpful Ruby scripts for simplifying your test
T
toby 已提交
83 84
workflow. They are purely optional and are not required to make use of Unity.

85 86 87 88 89

## How to Create A Test File

Test files are C files. Most often you will create a single test file for each C
module that you want to test. The test file should include unity.h and the
T
toby 已提交
90 91
header for your C module to be tested.

92 93 94 95
Next, a test file will include a `setUp()` and `tearDown()` function. The setUp
function can contain anything you would like to run before each test. The
tearDown function can contain anything you would like to run after each test.
Both functions accept no arguments and return nothing. You may leave either or
96 97 98
both of these blank if you have no need for them.

If you're using Ceedling or the test runner generator script, you may leave these off
99 100
completely. Not sure? Give it a try. If you compiler complains that it can't
find setUp or tearDown when it links, you'll know you need to at least include
T
toby 已提交
101 102
an empty function for these.

103
The majority of the file will be a series of test functions. Test functions
104
follow the convention of starting with the word "test_" or "spec_". You don't HAVE
105
to name them this way, but it makes it clear what functions are tests for other
106
developers.  Also, the automated scripts that come with Unity or Ceedling will default
107
to looking for test functions to be prefixed this way. Test functions take no arguments
108
and return nothing. All test accounting is handled internally in Unity.
T
toby 已提交
109

110 111 112 113
Finally, at the bottom of your test file, you will write a `main()` function.
This function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and
finally `UNITY_END()`.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`
T
toby 已提交
114 115
call.

116 117
Remembering to add each test to the main function can get to be tedious. If you
enjoy using helper scripts in your build process, you might consider making use
K
Kyle Krueger 已提交
118 119 120 121
of our handy [generate_test_runner.rb](../auto/generate_test_runner.rb) script.
This will create the main function and all the calls for you, assuming that you
have followed the suggested naming conventions. In this case, there is no need
for you to include the main function in your test file at all.
T
toby 已提交
122

123
When you're done, your test file will look something like this:
T
toby 已提交
124 125

```C
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include "unity.h"
#include "file_to_test.h"

void setUp(void) {
    // set stuff up here
}

void tearDown(void) {
    // clean stuff up here
}

void test_function_should_doBlahAndBlah(void) {
    //test stuff
}

void test_function_should_doAlsoDoBlah(void) {
    //more test stuff
}

K
Kyle Krueger 已提交
145
// not needed when using generate_test_runner.rb
146 147 148 149 150 151
int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_function_should_doBlahAndBlah);
    RUN_TEST(test_function_should_doAlsoDoBlah);
    return UNITY_END();
}
T
toby 已提交
152 153
```

D
Deryew 已提交
154
It's possible that you will need more customization than this, eventually.
155 156 157
For that sort of thing, you're going to want to look at the configuration guide.
This should be enough to get you going, though.

158 159
### Running Test Functions
When writing your own `main()` functions, for a test-runner. There are two ways
160
to execute the test.
161 162 163 164 165 166 167 168 169 170 171 172 173

The classic variant
``` c
RUN_TEST(func, linenum)
```
or its simpler replacement that starts at the beginning of the function.
``` c
RUN_TEST(func)
```
These macros perform the necessary setup before the test is called and
handles cleanup and result tabulation afterwards.

### Ignoring Test Functions
174 175
There 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
returned to the caller of the test, and no failures will be returned.
This is useful when your test runners are automatically generated.

``` c
TEST_IGNORE()
```

Ignore this test and return immediately

``` c
TEST_IGNORE_MESSAGE (message)
```

Ignore this test and return immediately. Output a message stating why the test was ignored.

### Aborting Tests
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.  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.

    TEST_PROTECT()

Setup and Catch macro

    TEST_ABORT()

Abort Test macro

Example:

    main()
    {
        if (TEST_PROTECT())
        {
            MyTest();
        }
    }

If MyTest calls `TEST_ABORT`, program control will immediately return to `TEST_PROTECT` with a return value of zero.


T
toby 已提交
215 216

## How to Build and Run A Test File
217 218 219 220 221

This is the single biggest challenge to picking up a new unit testing framework,
at least in a language like C or C++. 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"?). While this feature is usually a good thing, it
T
toby 已提交
222 223
can make testing more challenging.

224 225
You have two really good options for toolchains. Depending on where you're
coming from, it might surprise you that neither of these options is running the
T
toby 已提交
226 227 228 229 230 231 232
unit tests on your hardware.
There are many reasons for this, but here's a short version:
- On hardware, you have too many constraints (processing power, memory, etc),
- On hardware, you don't have complete control over all registers,
- On hardware, unit testing is more challenging,
- Unit testing isn't System testing. Keep them separate.

233 234 235 236 237
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. Either is a good option. Native apps have
the advantages of being faster and easier to set up. Simulator apps have the
advantage of working with the same compiler as your target application. The
T
toby 已提交
238 239
options for configuring these are discussed in the configuration guide.

240
To get either to work, you might need to make a few changes to the file
T
toby 已提交
241 242
containing your register set (discussed later).

243 244 245 246 247
In either case, a test is built by linking unity, the test file, and the C
file(s) being tested. These files create an executable which can be run as the
test set for that module. Then, this process is repeated for the next test file.
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
T
toby 已提交
248
our final release!
M
Mark VanderVoord 已提交
249 250 251


*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*