135375f98Sopenharmony_ci# Unity - Getting Started 235375f98Sopenharmony_ci 335375f98Sopenharmony_ci## Welcome 435375f98Sopenharmony_ci 535375f98Sopenharmony_ciCongratulations. 635375f98Sopenharmony_ciYou're now the proud owner of your very own pile of bits! 735375f98Sopenharmony_ciWhat are you going to do with all these ones and zeros? 835375f98Sopenharmony_ciThis document should be able to help you decide just that. 935375f98Sopenharmony_ci 1035375f98Sopenharmony_ciUnity is a unit test framework. 1135375f98Sopenharmony_ciThe goal has been to keep it small and functional. 1235375f98Sopenharmony_ciThe core Unity test framework is three files: a single C file and a couple header files. 1335375f98Sopenharmony_ciThese team up to provide functions and macros to make testing easier. 1435375f98Sopenharmony_ci 1535375f98Sopenharmony_ciUnity was designed to be cross-platform. 1635375f98Sopenharmony_ciIt works hard to stick with C standards while still providing support for the many embedded C compilers that bend the rules. 1735375f98Sopenharmony_ciUnity has been used with many compilers, including GCC, IAR, Clang, Green Hills, Microchip, and MS Visual Studio. 1835375f98Sopenharmony_ciIt's not much work to get it to work with a new target. 1935375f98Sopenharmony_ci 2035375f98Sopenharmony_ci### Overview of the Documents 2135375f98Sopenharmony_ci 2235375f98Sopenharmony_ci#### Unity Assertions reference 2335375f98Sopenharmony_ci 2435375f98Sopenharmony_ciThis document will guide you through all the assertion options provided by Unity. 2535375f98Sopenharmony_ciThis is going to be your unit testing bread and butter. 2635375f98Sopenharmony_ciYou'll spend more time with assertions than any other part of Unity. 2735375f98Sopenharmony_ci 2835375f98Sopenharmony_ci#### Unity Assertions Cheat Sheet 2935375f98Sopenharmony_ci 3035375f98Sopenharmony_ciThis document contains an abridged summary of the assertions described in the previous document. 3135375f98Sopenharmony_ciIt's perfect for printing and referencing while you familiarize yourself with Unity's options. 3235375f98Sopenharmony_ci 3335375f98Sopenharmony_ci#### Unity Configuration Guide 3435375f98Sopenharmony_ci 3535375f98Sopenharmony_ciThis document is the one to reference when you are going to use Unity with a new target or compiler. 3635375f98Sopenharmony_ciIt'll guide you through the configuration options and will help you customize your testing experience to meet your needs. 3735375f98Sopenharmony_ci 3835375f98Sopenharmony_ci#### Unity Helper Scripts 3935375f98Sopenharmony_ci 4035375f98Sopenharmony_ciThis document describes the helper scripts that are available for simplifying your testing workflow. 4135375f98Sopenharmony_ciIt describes the collection of optional Ruby scripts included in the auto directory of your Unity installation. 4235375f98Sopenharmony_ciNeither Ruby nor these scripts are necessary for using Unity. 4335375f98Sopenharmony_ciThey are provided as a convenience for those who wish to use them. 4435375f98Sopenharmony_ci 4535375f98Sopenharmony_ci#### Unity License 4635375f98Sopenharmony_ci 4735375f98Sopenharmony_ciWhat's an open source project without a license file? 4835375f98Sopenharmony_ciThis brief document describes the terms you're agreeing to when you use this software. 4935375f98Sopenharmony_ciBasically, 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. 5035375f98Sopenharmony_ci 5135375f98Sopenharmony_ci### Overview of the Folders 5235375f98Sopenharmony_ci 5335375f98Sopenharmony_ciIf 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. 5435375f98Sopenharmony_ciDon't worry, Unity itself is very small. 5535375f98Sopenharmony_ciThe rest of it is just there to make your life easier. 5635375f98Sopenharmony_ciYou can ignore it or use it at your convenience. 5735375f98Sopenharmony_ciHere's an overview of everything in the project. 5835375f98Sopenharmony_ci 5935375f98Sopenharmony_ci- `src` - This is the code you care about! This folder contains a C file and two header files. 6035375f98Sopenharmony_ci These three files _are_ Unity. 6135375f98Sopenharmony_ci- `docs` - You're reading this document, so it's possible you have found your way into this folder already. 6235375f98Sopenharmony_ci This is where all the handy documentation can be found. 6335375f98Sopenharmony_ci- `examples` - This contains a few examples of using Unity. 6435375f98Sopenharmony_ci- `extras` - These are optional add ons to Unity that are not part of the core project. 6535375f98Sopenharmony_ci If you've reached us through James Grenning's book, you're going to want to look here. 6635375f98Sopenharmony_ci- `test` - This is how Unity and its scripts are all tested. 6735375f98Sopenharmony_ci If you're just using Unity, you'll likely never need to go in here. 6835375f98Sopenharmony_ci 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. 6935375f98Sopenharmony_ci- `auto` - Here you will find helpful Ruby scripts for simplifying your test workflow. 7035375f98Sopenharmony_ci They are purely optional and are not required to make use of Unity. 7135375f98Sopenharmony_ci 7235375f98Sopenharmony_ci## How to Create A Test File 7335375f98Sopenharmony_ci 7435375f98Sopenharmony_ciTest files are C files. 7535375f98Sopenharmony_ciMost often you will create a single test file for each C module that you want to test. 7635375f98Sopenharmony_ciThe test file should include unity.h and the header for your C module to be tested. 7735375f98Sopenharmony_ci 7835375f98Sopenharmony_ciNext, a test file will include a `setUp()` and `tearDown()` function. 7935375f98Sopenharmony_ciThe setUp function can contain anything you would like to run before each test. 8035375f98Sopenharmony_ciThe tearDown function can contain anything you would like to run after each test. 8135375f98Sopenharmony_ciBoth functions accept no arguments and return nothing. 8235375f98Sopenharmony_ciYou may leave either or both of these blank if you have no need for them. 8335375f98Sopenharmony_ci 8435375f98Sopenharmony_ciIf you're using Ceedling or the test runner generator script, you may leave these off completely. 8535375f98Sopenharmony_ciNot sure? 8635375f98Sopenharmony_ciGive it a try. 8735375f98Sopenharmony_ciIf 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. 8835375f98Sopenharmony_ci 8935375f98Sopenharmony_ciThe majority of the file will be a series of test functions. 9035375f98Sopenharmony_ciTest functions follow the convention of starting with the word "test_" or "spec_". 9135375f98Sopenharmony_ciYou don't HAVE to name them this way, but it makes it clear what functions are tests for other developers. 9235375f98Sopenharmony_ciAlso, the automated scripts that come with Unity or Ceedling will default to looking for test functions to be prefixed this way. 9335375f98Sopenharmony_ciTest functions take no arguments and return nothing. All test accounting is handled internally in Unity. 9435375f98Sopenharmony_ci 9535375f98Sopenharmony_ciFinally, at the bottom of your test file, you will write a `main()` function. 9635375f98Sopenharmony_ciThis function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and finally `UNITY_END()`. 9735375f98Sopenharmony_ciThis 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. 9835375f98Sopenharmony_ci 9935375f98Sopenharmony_ciRemembering to add each test to the main function can get to be tedious. 10035375f98Sopenharmony_ciIf you enjoy using helper scripts in your build process, you might consider making use of our handy [generate_test_runner.rb][] script. 10135375f98Sopenharmony_ciThis will create the main function and all the calls for you, assuming that you have followed the suggested naming conventions. 10235375f98Sopenharmony_ciIn this case, there is no need for you to include the main function in your test file at all. 10335375f98Sopenharmony_ci 10435375f98Sopenharmony_ciWhen you're done, your test file will look something like this: 10535375f98Sopenharmony_ci 10635375f98Sopenharmony_ci```C 10735375f98Sopenharmony_ci#include "unity.h" 10835375f98Sopenharmony_ci#include "file_to_test.h" 10935375f98Sopenharmony_ci 11035375f98Sopenharmony_civoid setUp(void) { 11135375f98Sopenharmony_ci // set stuff up here 11235375f98Sopenharmony_ci} 11335375f98Sopenharmony_ci 11435375f98Sopenharmony_civoid tearDown(void) { 11535375f98Sopenharmony_ci // clean stuff up here 11635375f98Sopenharmony_ci} 11735375f98Sopenharmony_ci 11835375f98Sopenharmony_civoid test_function_should_doBlahAndBlah(void) { 11935375f98Sopenharmony_ci //test stuff 12035375f98Sopenharmony_ci} 12135375f98Sopenharmony_ci 12235375f98Sopenharmony_civoid test_function_should_doAlsoDoBlah(void) { 12335375f98Sopenharmony_ci //more test stuff 12435375f98Sopenharmony_ci} 12535375f98Sopenharmony_ci 12635375f98Sopenharmony_ci// not needed when using generate_test_runner.rb 12735375f98Sopenharmony_ciint main(void) { 12835375f98Sopenharmony_ci UNITY_BEGIN(); 12935375f98Sopenharmony_ci RUN_TEST(test_function_should_doBlahAndBlah); 13035375f98Sopenharmony_ci RUN_TEST(test_function_should_doAlsoDoBlah); 13135375f98Sopenharmony_ci return UNITY_END(); 13235375f98Sopenharmony_ci} 13335375f98Sopenharmony_ci``` 13435375f98Sopenharmony_ci 13535375f98Sopenharmony_ciIt's possible that you will need more customization than this, eventually. 13635375f98Sopenharmony_ciFor that sort of thing, you're going to want to look at the configuration guide. 13735375f98Sopenharmony_ciThis should be enough to get you going, though. 13835375f98Sopenharmony_ci 13935375f98Sopenharmony_ci### Running Test Functions 14035375f98Sopenharmony_ci 14135375f98Sopenharmony_ciWhen writing your own `main()` functions, for a test-runner. 14235375f98Sopenharmony_ciThere are two ways to execute the test. 14335375f98Sopenharmony_ci 14435375f98Sopenharmony_ciThe classic variant 14535375f98Sopenharmony_ci 14635375f98Sopenharmony_ci``` c 14735375f98Sopenharmony_ciRUN_TEST(func, linenum) 14835375f98Sopenharmony_ci``` 14935375f98Sopenharmony_ci 15035375f98Sopenharmony_ciOr its simpler replacement that starts at the beginning of the function. 15135375f98Sopenharmony_ci 15235375f98Sopenharmony_ci``` c 15335375f98Sopenharmony_ciRUN_TEST(func) 15435375f98Sopenharmony_ci``` 15535375f98Sopenharmony_ci 15635375f98Sopenharmony_ciThese macros perform the necessary setup before the test is called and handles clean-up and result tabulation afterwards. 15735375f98Sopenharmony_ci 15835375f98Sopenharmony_ci### Ignoring Test Functions 15935375f98Sopenharmony_ci 16035375f98Sopenharmony_ciThere are times when a test is incomplete or not valid for some reason. 16135375f98Sopenharmony_ciAt these times, TEST_IGNORE can be called. 16235375f98Sopenharmony_ciControl will immediately be returned to the caller of the test, and no failures will be returned. 16335375f98Sopenharmony_ciThis is useful when your test runners are automatically generated. 16435375f98Sopenharmony_ci 16535375f98Sopenharmony_ci``` c 16635375f98Sopenharmony_ciTEST_IGNORE() 16735375f98Sopenharmony_ci``` 16835375f98Sopenharmony_ci 16935375f98Sopenharmony_ciIgnore this test and return immediately 17035375f98Sopenharmony_ci 17135375f98Sopenharmony_ci```c 17235375f98Sopenharmony_ciTEST_IGNORE_MESSAGE (message) 17335375f98Sopenharmony_ci``` 17435375f98Sopenharmony_ci 17535375f98Sopenharmony_ciIgnore this test and return immediately. 17635375f98Sopenharmony_ciOutput a message stating why the test was ignored. 17735375f98Sopenharmony_ci 17835375f98Sopenharmony_ci### Aborting Tests 17935375f98Sopenharmony_ci 18035375f98Sopenharmony_ciThere 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. 18135375f98Sopenharmony_ciA pair of macros support this functionality in Unity. 18235375f98Sopenharmony_ciThe first `TEST_PROTECT` sets up the feature, and handles emergency abort cases. 18335375f98Sopenharmony_ci`TEST_ABORT` can then be used at any time within the tests to return to the last `TEST_PROTECT` call. 18435375f98Sopenharmony_ci 18535375f98Sopenharmony_ci```c 18635375f98Sopenharmony_ci TEST_PROTECT() 18735375f98Sopenharmony_ci``` 18835375f98Sopenharmony_ci 18935375f98Sopenharmony_ciSetup and Catch macro 19035375f98Sopenharmony_ci 19135375f98Sopenharmony_ci```c 19235375f98Sopenharmony_ci TEST_ABORT() 19335375f98Sopenharmony_ci``` 19435375f98Sopenharmony_ci 19535375f98Sopenharmony_ciAbort Test macro 19635375f98Sopenharmony_ci 19735375f98Sopenharmony_ciExample: 19835375f98Sopenharmony_ci 19935375f98Sopenharmony_ci```c 20035375f98Sopenharmony_ci main() 20135375f98Sopenharmony_ci { 20235375f98Sopenharmony_ci if (TEST_PROTECT()) 20335375f98Sopenharmony_ci { 20435375f98Sopenharmony_ci MyTest(); 20535375f98Sopenharmony_ci } 20635375f98Sopenharmony_ci } 20735375f98Sopenharmony_ci``` 20835375f98Sopenharmony_ci 20935375f98Sopenharmony_ciIf MyTest calls `TEST_ABORT`, program control will immediately return to `TEST_PROTECT` with a return value of zero. 21035375f98Sopenharmony_ci 21135375f98Sopenharmony_ci## How to Build and Run A Test File 21235375f98Sopenharmony_ci 21335375f98Sopenharmony_ciThis is the single biggest challenge to picking up a new unit testing framework, at least in a language like C or C++. 21435375f98Sopenharmony_ciThese 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"?). 21535375f98Sopenharmony_ciWhile this feature is usually a good thing, it can make testing more challenging. 21635375f98Sopenharmony_ci 21735375f98Sopenharmony_ciYou have two really good options for toolchains. 21835375f98Sopenharmony_ciDepending on where you're coming from, it might surprise you that neither of these options is running the unit tests on your hardware. 21935375f98Sopenharmony_ciThere are many reasons for this, but here's a short version: 22035375f98Sopenharmony_ci 22135375f98Sopenharmony_ci- On hardware, you have too many constraints (processing power, memory, etc), 22235375f98Sopenharmony_ci- On hardware, you don't have complete control over all registers, 22335375f98Sopenharmony_ci- On hardware, unit testing is more challenging, 22435375f98Sopenharmony_ci- Unit testing isn't System testing. Keep them separate. 22535375f98Sopenharmony_ci 22635375f98Sopenharmony_ciInstead 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. 22735375f98Sopenharmony_ciEither is a good option. 22835375f98Sopenharmony_ciNative apps have the advantages of being faster and easier to set up. 22935375f98Sopenharmony_ciSimulator apps have the advantage of working with the same compiler as your target application. 23035375f98Sopenharmony_ciThe options for configuring these are discussed in the configuration guide. 23135375f98Sopenharmony_ci 23235375f98Sopenharmony_ciTo get either to work, you might need to make a few changes to the file containing your register set (discussed later). 23335375f98Sopenharmony_ci 23435375f98Sopenharmony_ciIn either case, a test is built by linking unity, the test file, and the C file(s) being tested. 23535375f98Sopenharmony_ciThese files create an executable which can be run as the test set for that module. 23635375f98Sopenharmony_ciThen, this process is repeated for the next test file. 23735375f98Sopenharmony_ciThis 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! 23835375f98Sopenharmony_ci 23935375f98Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org][]* 24035375f98Sopenharmony_ci 24135375f98Sopenharmony_ci[generate_test_runner.rb]: ../auto/generate_test_runner.rb 24235375f98Sopenharmony_ci[ThrowTheSwitch.org]: https://throwtheswitch.org 243