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. If you're using a compiler 97that is configured to make these functions optional, you may leave them off 98completely. Not sure? Give it a try. If you compiler complains that it can't 99find setUp or tearDown when it links, you'll know you need to at least include 100an empty function for these. 101 102The majority of the file will be a series of test functions. Test functions 103follow the convention of starting with the word "test" or "spec". You don't HAVE 104to name them this way, but it makes it clear what functions are tests for other 105developers. Test functions take no arguments and return nothing. All test 106accounting is handled internally in Unity. 107 108Finally, at the bottom of your test file, you will write a `main()` function. 109This function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and 110finally `UNITY_END()`.This is what will actually trigger each of those test 111functions to run, so it is important that each function gets its own `RUN_TEST` 112call. 113 114Remembering to add each test to the main function can get to be tedious. If you 115enjoy using helper scripts in your build process, you might consider making use 116of our handy generate_test_runner.rb script. This will create the main function 117and all the calls for you, assuming that you have followed the suggested naming 118conventions. In this case, there is no need for you to include the main function 119in your test file at all. 120 121When you're done, your test file will look something like this: 122 123```C 124#include "unity.h" 125#include "file_to_test.h" 126 127void setUp(void) { 128 // set stuff up here 129} 130 131void tearDown(void) { 132 // clean stuff up here 133} 134 135void test_function_should_doBlahAndBlah(void) { 136 //test stuff 137} 138 139void test_function_should_doAlsoDoBlah(void) { 140 //more test stuff 141} 142 143int main(void) { 144 UNITY_BEGIN(); 145 RUN_TEST(test_function_should_doBlahAndBlah); 146 RUN_TEST(test_function_should_doAlsoDoBlah); 147 return UNITY_END(); 148} 149``` 150 151It's possible that you will require more customization than this, eventually. 152For that sort of thing, you're going to want to look at the configuration guide. 153This should be enough to get you going, though. 154 155 156## How to Build and Run A Test File 157 158This is the single biggest challenge to picking up a new unit testing framework, 159at least in a language like C or C++. These languages are REALLY good at getting 160you "close to the metal" (why is the phrase metal? Wouldn't it be more accurate 161to say "close to the silicon"?). While this feature is usually a good thing, it 162can make testing more challenging. 163 164You have two really good options for toolchains. Depending on where you're 165coming from, it might surprise you that neither of these options is running the 166unit tests on your hardware. 167There are many reasons for this, but here's a short version: 168- On hardware, you have too many constraints (processing power, memory, etc), 169- On hardware, you don't have complete control over all registers, 170- On hardware, unit testing is more challenging, 171- Unit testing isn't System testing. Keep them separate. 172 173Instead of running your tests on your actual hardware, most developers choose to 174develop them as native applications (using gcc or MSVC for example) or as 175applications running on a simulator. Either is a good option. Native apps have 176the advantages of being faster and easier to set up. Simulator apps have the 177advantage of working with the same compiler as your target application. The 178options for configuring these are discussed in the configuration guide. 179 180To get either to work, you might need to make a few changes to the file 181containing your register set (discussed later). 182 183In either case, a test is built by linking unity, the test file, and the C 184file(s) being tested. These files create an executable which can be run as the 185test set for that module. Then, this process is repeated for the next test file. 186This flexibility of separating tests into individual executables allows us to 187much more thoroughly unit test our system and it keeps all the test code out of 188our final release! 189 190 191*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)* 192