1# Unity - Getting Started 2 3## Welcome 4 5Congratulations. 6You're now the proud owner of your very own pile of bits! 7What are you going to do with all these ones and zeros? 8This document should be able to help you decide just that. 9 10Unity is a unit test framework. 11The goal has been to keep it small and functional. 12The core Unity test framework is three files: a single C file and a couple header files. 13These team up to provide functions and macros to make testing easier. 14 15Unity was designed to be cross-platform. 16It works hard to stick with C standards while still providing support for the many embedded C compilers that bend the rules. 17Unity has been used with many compilers, including GCC, IAR, Clang, Green Hills, Microchip, and MS Visual Studio. 18It'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 24This document will guide you through all the assertion options provided by Unity. 25This is going to be your unit testing bread and butter. 26You'll spend more time with assertions than any other part of Unity. 27 28#### Unity Assertions Cheat Sheet 29 30This document contains an abridged summary of the assertions described in the previous document. 31It's perfect for printing and referencing while you familiarize yourself with Unity's options. 32 33#### Unity Configuration Guide 34 35This document is the one to reference when you are going to use Unity with a new target or compiler. 36It'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 40This document describes the helper scripts that are available for simplifying your testing workflow. 41It describes the collection of optional Ruby scripts included in the auto directory of your Unity installation. 42Neither Ruby nor these scripts are necessary for using Unity. 43They are provided as a convenience for those who wish to use them. 44 45#### Unity License 46 47What's an open source project without a license file? 48This brief document describes the terms you're agreeing to when you use this software. 49Basically, 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 53If 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. 54Don't worry, Unity itself is very small. 55The rest of it is just there to make your life easier. 56You can ignore it or use it at your convenience. 57Here'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 74Test files are C files. 75Most often you will create a single test file for each C module that you want to test. 76The test file should include unity.h and the header for your C module to be tested. 77 78Next, a test file will include a `setUp()` and `tearDown()` function. 79The setUp function can contain anything you would like to run before each test. 80The tearDown function can contain anything you would like to run after each test. 81Both functions accept no arguments and return nothing. 82You may leave either or both of these blank if you have no need for them. 83 84If you're using Ceedling or the test runner generator script, you may leave these off completely. 85Not sure? 86Give it a try. 87If 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 89The majority of the file will be a series of test functions. 90Test functions follow the convention of starting with the word "test_" or "spec_". 91You don't HAVE to name them this way, but it makes it clear what functions are tests for other developers. 92Also, the automated scripts that come with Unity or Ceedling will default to looking for test functions to be prefixed this way. 93Test functions take no arguments and return nothing. All test accounting is handled internally in Unity. 94 95Finally, at the bottom of your test file, you will write a `main()` function. 96This function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and finally `UNITY_END()`. 97This 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 99Remembering to add each test to the main function can get to be tedious. 100If you enjoy using helper scripts in your build process, you might consider making use of our handy [generate_test_runner.rb][] script. 101This will create the main function and all the calls for you, assuming that you have followed the suggested naming conventions. 102In this case, there is no need for you to include the main function in your test file at all. 103 104When 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 110void setUp(void) { 111 // set stuff up here 112} 113 114void tearDown(void) { 115 // clean stuff up here 116} 117 118void test_function_should_doBlahAndBlah(void) { 119 //test stuff 120} 121 122void test_function_should_doAlsoDoBlah(void) { 123 //more test stuff 124} 125 126// not needed when using generate_test_runner.rb 127int 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 135It's possible that you will need more customization than this, eventually. 136For that sort of thing, you're going to want to look at the configuration guide. 137This should be enough to get you going, though. 138 139### Running Test Functions 140 141When writing your own `main()` functions, for a test-runner. 142There are two ways to execute the test. 143 144The classic variant 145 146``` c 147RUN_TEST(func, linenum) 148``` 149 150Or its simpler replacement that starts at the beginning of the function. 151 152``` c 153RUN_TEST(func) 154``` 155 156These macros perform the necessary setup before the test is called and handles clean-up and result tabulation afterwards. 157 158### Ignoring Test Functions 159 160There are times when a test is incomplete or not valid for some reason. 161At these times, TEST_IGNORE can be called. 162Control will immediately be returned to the caller of the test, and no failures will be returned. 163This is useful when your test runners are automatically generated. 164 165``` c 166TEST_IGNORE() 167``` 168 169Ignore this test and return immediately 170 171```c 172TEST_IGNORE_MESSAGE (message) 173``` 174 175Ignore this test and return immediately. 176Output a message stating why the test was ignored. 177 178### Aborting Tests 179 180There 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. 181A pair of macros support this functionality in Unity. 182The 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 189Setup and Catch macro 190 191```c 192 TEST_ABORT() 193``` 194 195Abort Test macro 196 197Example: 198 199```c 200 main() 201 { 202 if (TEST_PROTECT()) 203 { 204 MyTest(); 205 } 206 } 207``` 208 209If 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 213This is the single biggest challenge to picking up a new unit testing framework, at least in a language like C or C++. 214These 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"?). 215While this feature is usually a good thing, it can make testing more challenging. 216 217You have two really good options for toolchains. 218Depending on where you're coming from, it might surprise you that neither of these options is running the unit tests on your hardware. 219There 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 226Instead 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. 227Either is a good option. 228Native apps have the advantages of being faster and easier to set up. 229Simulator apps have the advantage of working with the same compiler as your target application. 230The options for configuring these are discussed in the configuration guide. 231 232To get either to work, you might need to make a few changes to the file containing your register set (discussed later). 233 234In either case, a test is built by linking unity, the test file, and the C file(s) being tested. 235These files create an executable which can be run as the test set for that module. 236Then, this process is repeated for the next test file. 237This 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 243