19750e409Sopenharmony_ci# Unity - Getting Started 29750e409Sopenharmony_ci 39750e409Sopenharmony_ci## Welcome 49750e409Sopenharmony_ci 59750e409Sopenharmony_ciCongratulations. You're now the proud owner of your very own pile of bits! What 69750e409Sopenharmony_ciare you going to do with all these ones and zeros? This document should be able 79750e409Sopenharmony_cito help you decide just that. 89750e409Sopenharmony_ci 99750e409Sopenharmony_ciUnity is a unit test framework. The goal has been to keep it small and 109750e409Sopenharmony_cifunctional. The core Unity test framework is three files: a single C file and a 119750e409Sopenharmony_cicouple header files. These team up to provide functions and macros to make 129750e409Sopenharmony_citesting easier. 139750e409Sopenharmony_ci 149750e409Sopenharmony_ciUnity was designed to be cross platform. It works hard to stick with C standards 159750e409Sopenharmony_ciwhile still providing support for the many embedded C compilers that bend the 169750e409Sopenharmony_cirules. Unity has been used with many compilers, including GCC, IAR, Clang, 179750e409Sopenharmony_ciGreen Hills, Microchip, and MS Visual Studio. It's not much work to get it to 189750e409Sopenharmony_ciwork with a new target. 199750e409Sopenharmony_ci 209750e409Sopenharmony_ci 219750e409Sopenharmony_ci### Overview of the Documents 229750e409Sopenharmony_ci 239750e409Sopenharmony_ci#### Unity Assertions reference 249750e409Sopenharmony_ci 259750e409Sopenharmony_ciThis document will guide you through all the assertion options provided by 269750e409Sopenharmony_ciUnity. This is going to be your unit testing bread and butter. You'll spend more 279750e409Sopenharmony_citime with assertions than any other part of Unity. 289750e409Sopenharmony_ci 299750e409Sopenharmony_ci 309750e409Sopenharmony_ci#### Unity Assertions Cheat Sheet 319750e409Sopenharmony_ci 329750e409Sopenharmony_ciThis document contains an abridged summary of the assertions described in the 339750e409Sopenharmony_ciprevious document. It's perfect for printing and referencing while you 349750e409Sopenharmony_cifamiliarize yourself with Unity's options. 359750e409Sopenharmony_ci 369750e409Sopenharmony_ci 379750e409Sopenharmony_ci#### Unity Configuration Guide 389750e409Sopenharmony_ci 399750e409Sopenharmony_ciThis document is the one to reference when you are going to use Unity with a new 409750e409Sopenharmony_citarget or compiler. It'll guide you through the configuration options and will 419750e409Sopenharmony_cihelp you customize your testing experience to meet your needs. 429750e409Sopenharmony_ci 439750e409Sopenharmony_ci 449750e409Sopenharmony_ci#### Unity Helper Scripts 459750e409Sopenharmony_ci 469750e409Sopenharmony_ciThis document describes the helper scripts that are available for simplifying 479750e409Sopenharmony_ciyour testing workflow. It describes the collection of optional Ruby scripts 489750e409Sopenharmony_ciincluded in the auto directory of your Unity installation. Neither Ruby nor 499750e409Sopenharmony_cithese scripts are necessary for using Unity. They are provided as a convenience 509750e409Sopenharmony_cifor those who wish to use them. 519750e409Sopenharmony_ci 529750e409Sopenharmony_ci 539750e409Sopenharmony_ci#### Unity License 549750e409Sopenharmony_ci 559750e409Sopenharmony_ciWhat's an open source project without a license file? This brief document 569750e409Sopenharmony_cidescribes the terms you're agreeing to when you use this software. Basically, we 579750e409Sopenharmony_ciwant it to be useful to you in whatever context you want to use it, but please 589750e409Sopenharmony_cidon't blame us if you run into problems. 599750e409Sopenharmony_ci 609750e409Sopenharmony_ci 619750e409Sopenharmony_ci### Overview of the Folders 629750e409Sopenharmony_ci 639750e409Sopenharmony_ciIf you have obtained Unity through Github or something similar, you might be 649750e409Sopenharmony_cisurprised by just how much stuff you suddenly have staring you in the face. 659750e409Sopenharmony_ciDon't worry, Unity itself is very small. The rest of it is just there to make 669750e409Sopenharmony_ciyour life easier. You can ignore it or use it at your convenience. Here's an 679750e409Sopenharmony_cioverview of everything in the project. 689750e409Sopenharmony_ci 699750e409Sopenharmony_ci- `src` - This is the code you care about! This folder contains a C file and two 709750e409Sopenharmony_ciheader files. These three files _are_ Unity. 719750e409Sopenharmony_ci- `docs` - You're reading this document, so it's possible you have found your way 729750e409Sopenharmony_ciinto this folder already. This is where all the handy documentation can be 739750e409Sopenharmony_cifound. 749750e409Sopenharmony_ci- `examples` - This contains a few examples of using Unity. 759750e409Sopenharmony_ci- `extras` - These are optional add ons to Unity that are not part of the core 769750e409Sopenharmony_ciproject. If you've reached us through James Grenning's book, you're going to 779750e409Sopenharmony_ciwant to look here. 789750e409Sopenharmony_ci- `test` - This is how Unity and its scripts are all tested. If you're just using 799750e409Sopenharmony_ciUnity, you'll likely never need to go in here. If you are the lucky team member 809750e409Sopenharmony_ciwho gets to port Unity to a new toolchain, this is a good place to verify 819750e409Sopenharmony_cieverything is configured properly. 829750e409Sopenharmony_ci- `auto` - Here you will find helpful Ruby scripts for simplifying your test 839750e409Sopenharmony_ciworkflow. They are purely optional and are not required to make use of Unity. 849750e409Sopenharmony_ci 859750e409Sopenharmony_ci 869750e409Sopenharmony_ci## How to Create A Test File 879750e409Sopenharmony_ci 889750e409Sopenharmony_ciTest files are C files. Most often you will create a single test file for each C 899750e409Sopenharmony_cimodule that you want to test. The test file should include unity.h and the 909750e409Sopenharmony_ciheader for your C module to be tested. 919750e409Sopenharmony_ci 929750e409Sopenharmony_ciNext, a test file will include a `setUp()` and `tearDown()` function. The setUp 939750e409Sopenharmony_cifunction can contain anything you would like to run before each test. The 949750e409Sopenharmony_citearDown function can contain anything you would like to run after each test. 959750e409Sopenharmony_ciBoth functions accept no arguments and return nothing. You may leave either or 969750e409Sopenharmony_ciboth of these blank if you have no need for them. If you're using a compiler 979750e409Sopenharmony_cithat is configured to make these functions optional, you may leave them off 989750e409Sopenharmony_cicompletely. Not sure? Give it a try. If you compiler complains that it can't 999750e409Sopenharmony_cifind setUp or tearDown when it links, you'll know you need to at least include 1009750e409Sopenharmony_cian empty function for these. 1019750e409Sopenharmony_ci 1029750e409Sopenharmony_ciThe majority of the file will be a series of test functions. Test functions 1039750e409Sopenharmony_cifollow the convention of starting with the word "test" or "spec". You don't HAVE 1049750e409Sopenharmony_cito name them this way, but it makes it clear what functions are tests for other 1059750e409Sopenharmony_cidevelopers. Test functions take no arguments and return nothing. All test 1069750e409Sopenharmony_ciaccounting is handled internally in Unity. 1079750e409Sopenharmony_ci 1089750e409Sopenharmony_ciFinally, at the bottom of your test file, you will write a `main()` function. 1099750e409Sopenharmony_ciThis function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and 1109750e409Sopenharmony_cifinally `UNITY_END()`.This is what will actually trigger each of those test 1119750e409Sopenharmony_cifunctions to run, so it is important that each function gets its own `RUN_TEST` 1129750e409Sopenharmony_cicall. 1139750e409Sopenharmony_ci 1149750e409Sopenharmony_ciRemembering to add each test to the main function can get to be tedious. If you 1159750e409Sopenharmony_cienjoy using helper scripts in your build process, you might consider making use 1169750e409Sopenharmony_ciof our handy generate_test_runner.rb script. This will create the main function 1179750e409Sopenharmony_ciand all the calls for you, assuming that you have followed the suggested naming 1189750e409Sopenharmony_ciconventions. In this case, there is no need for you to include the main function 1199750e409Sopenharmony_ciin your test file at all. 1209750e409Sopenharmony_ci 1219750e409Sopenharmony_ciWhen you're done, your test file will look something like this: 1229750e409Sopenharmony_ci 1239750e409Sopenharmony_ci```C 1249750e409Sopenharmony_ci#include "unity.h" 1259750e409Sopenharmony_ci#include "file_to_test.h" 1269750e409Sopenharmony_ci 1279750e409Sopenharmony_civoid setUp(void) { 1289750e409Sopenharmony_ci // set stuff up here 1299750e409Sopenharmony_ci} 1309750e409Sopenharmony_ci 1319750e409Sopenharmony_civoid tearDown(void) { 1329750e409Sopenharmony_ci // clean stuff up here 1339750e409Sopenharmony_ci} 1349750e409Sopenharmony_ci 1359750e409Sopenharmony_civoid test_function_should_doBlahAndBlah(void) { 1369750e409Sopenharmony_ci //test stuff 1379750e409Sopenharmony_ci} 1389750e409Sopenharmony_ci 1399750e409Sopenharmony_civoid test_function_should_doAlsoDoBlah(void) { 1409750e409Sopenharmony_ci //more test stuff 1419750e409Sopenharmony_ci} 1429750e409Sopenharmony_ci 1439750e409Sopenharmony_ciint main(void) { 1449750e409Sopenharmony_ci UNITY_BEGIN(); 1459750e409Sopenharmony_ci RUN_TEST(test_function_should_doBlahAndBlah); 1469750e409Sopenharmony_ci RUN_TEST(test_function_should_doAlsoDoBlah); 1479750e409Sopenharmony_ci return UNITY_END(); 1489750e409Sopenharmony_ci} 1499750e409Sopenharmony_ci``` 1509750e409Sopenharmony_ci 1519750e409Sopenharmony_ciIt's possible that you will require more customization than this, eventually. 1529750e409Sopenharmony_ciFor that sort of thing, you're going to want to look at the configuration guide. 1539750e409Sopenharmony_ciThis should be enough to get you going, though. 1549750e409Sopenharmony_ci 1559750e409Sopenharmony_ci 1569750e409Sopenharmony_ci## How to Build and Run A Test File 1579750e409Sopenharmony_ci 1589750e409Sopenharmony_ciThis is the single biggest challenge to picking up a new unit testing framework, 1599750e409Sopenharmony_ciat least in a language like C or C++. These languages are REALLY good at getting 1609750e409Sopenharmony_ciyou "close to the metal" (why is the phrase metal? Wouldn't it be more accurate 1619750e409Sopenharmony_cito say "close to the silicon"?). While this feature is usually a good thing, it 1629750e409Sopenharmony_cican make testing more challenging. 1639750e409Sopenharmony_ci 1649750e409Sopenharmony_ciYou have two really good options for toolchains. Depending on where you're 1659750e409Sopenharmony_cicoming from, it might surprise you that neither of these options is running the 1669750e409Sopenharmony_ciunit tests on your hardware. 1679750e409Sopenharmony_ciThere are many reasons for this, but here's a short version: 1689750e409Sopenharmony_ci- On hardware, you have too many constraints (processing power, memory, etc), 1699750e409Sopenharmony_ci- On hardware, you don't have complete control over all registers, 1709750e409Sopenharmony_ci- On hardware, unit testing is more challenging, 1719750e409Sopenharmony_ci- Unit testing isn't System testing. Keep them separate. 1729750e409Sopenharmony_ci 1739750e409Sopenharmony_ciInstead of running your tests on your actual hardware, most developers choose to 1749750e409Sopenharmony_cidevelop them as native applications (using gcc or MSVC for example) or as 1759750e409Sopenharmony_ciapplications running on a simulator. Either is a good option. Native apps have 1769750e409Sopenharmony_cithe advantages of being faster and easier to set up. Simulator apps have the 1779750e409Sopenharmony_ciadvantage of working with the same compiler as your target application. The 1789750e409Sopenharmony_cioptions for configuring these are discussed in the configuration guide. 1799750e409Sopenharmony_ci 1809750e409Sopenharmony_ciTo get either to work, you might need to make a few changes to the file 1819750e409Sopenharmony_cicontaining your register set (discussed later). 1829750e409Sopenharmony_ci 1839750e409Sopenharmony_ciIn either case, a test is built by linking unity, the test file, and the C 1849750e409Sopenharmony_cifile(s) being tested. These files create an executable which can be run as the 1859750e409Sopenharmony_citest set for that module. Then, this process is repeated for the next test file. 1869750e409Sopenharmony_ciThis flexibility of separating tests into individual executables allows us to 1879750e409Sopenharmony_cimuch more thoroughly unit test our system and it keeps all the test code out of 1889750e409Sopenharmony_ciour final release! 1899750e409Sopenharmony_ci 1909750e409Sopenharmony_ci 1919750e409Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)* 192