cJSON
[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)
Brought by drakestapleton
cJSON / tests / unity / docs / UnityGettingStartedGuide.md
| 1 | # Unity - Getting Started |
| 2 | |
| 3 | ## Welcome |
| 4 | |
| 5 | Congratulations. You're now the proud owner of your very own pile of bits! What |
| 6 | are you going to do with all these ones and zeros? This document should be able |
| 7 | to help you decide just that. |
| 8 | |
| 9 | Unity is a unit test framework. The goal has been to keep it small and |
| 10 | functional. The core Unity test framework is three files: a single C file and a |
| 11 | couple header files. These team up to provide functions and macros to make |
| 12 | testing easier. |
| 13 | |
| 14 | Unity was designed to be cross platform. It works hard to stick with C standards |
| 15 | while still providing support for the many embedded C compilers that bend the |
| 16 | rules. Unity has been used with many compilers, including GCC, IAR, Clang, |
| 17 | Green Hills, Microchip, and MS Visual Studio. It's not much work to get it to |
| 18 | work with a new target. |
| 19 | |
| 20 | |
| 21 | ### Overview of the Documents |
| 22 | |
| 23 | #### Unity Assertions reference |
| 24 | |
| 25 | This document will guide you through all the assertion options provided by |
| 26 | Unity. This is going to be your unit testing bread and butter. You'll spend more |
| 27 | time with assertions than any other part of Unity. |
| 28 | |
| 29 | |
| 30 | #### Unity Assertions Cheat Sheet |
| 31 | |
| 32 | This document contains an abridged summary of the assertions described in the |
| 33 | previous document. It's perfect for printing and referencing while you |
| 34 | familiarize yourself with Unity's options. |
| 35 | |
| 36 | |
| 37 | #### Unity Configuration Guide |
| 38 | |
| 39 | This document is the one to reference when you are going to use Unity with a new |
| 40 | target or compiler. It'll guide you through the configuration options and will |
| 41 | help you customize your testing experience to meet your needs. |
| 42 | |
| 43 | |
| 44 | #### Unity Helper Scripts |
| 45 | |
| 46 | This document describes the helper scripts that are available for simplifying |
| 47 | your testing workflow. It describes the collection of optional Ruby scripts |
| 48 | included in the auto directory of your Unity installation. Neither Ruby nor |
| 49 | these scripts are necessary for using Unity. They are provided as a convenience |
| 50 | for those who wish to use them. |
| 51 | |
| 52 | |
| 53 | #### Unity License |
| 54 | |
| 55 | What's an open source project without a license file? This brief document |
| 56 | describes the terms you're agreeing to when you use this software. Basically, we |
| 57 | want it to be useful to you in whatever context you want to use it, but please |
| 58 | don't blame us if you run into problems. |
| 59 | |
| 60 | |
| 61 | ### Overview of the Folders |
| 62 | |
| 63 | If you have obtained Unity through Github or something similar, you might be |
| 64 | surprised by just how much stuff you suddenly have staring you in the face. |
| 65 | Don't worry, Unity itself is very small. The rest of it is just there to make |
| 66 | your life easier. You can ignore it or use it at your convenience. Here's an |
| 67 | overview of everything in the project. |
| 68 | |
| 69 | - `src` - This is the code you care about! This folder contains a C file and two |
| 70 | header files. These three files _are_ Unity. |
| 71 | - `docs` - You're reading this document, so it's possible you have found your way |
| 72 | into this folder already. This is where all the handy documentation can be |
| 73 | found. |
| 74 | - `examples` - This contains a few examples of using Unity. |
| 75 | - `extras` - These are optional addons to Unity that are not part of the core |
| 76 | project. If you've reached us through James Grenning's book, you're going to |
| 77 | want to look here. |
| 78 | - `test` - This is how Unity and its scripts are all tested. If you're just using |
| 79 | Unity, you'll likely never need to go in here. If you are the lucky team member |
| 80 | who gets to port Unity to a new toolchain, this is a good place to verify |
| 81 | everything is configured properly. |
| 82 | - `auto` - Here you will find helpful Ruby scripts for simplifying your test |
| 83 | workflow. They are purely optional and are not required to make use of Unity. |
| 84 | |
| 85 | |
| 86 | ## How to Create A Test File |
| 87 | |
| 88 | Test files are C files. Most often you will create a single test file for each C |
| 89 | module that you want to test. The test file should include unity.h and the |
| 90 | header for your C module to be tested. |
| 91 | |
| 92 | Next, a test file will include a `setUp()` and `tearDown()` function. The setUp |
| 93 | function can contain anything you would like to run before each test. The |
| 94 | tearDown function can contain anything you would like to run after each test. |
| 95 | Both functions accept no arguments and return nothing. You may leave either or |
| 96 | both of these blank if you have no need for them. If you're using a compiler |
| 97 | that is configured to make these functions optional, you may leave them off |
| 98 | completely. Not sure? Give it a try. If you compiler complains that it can't |
| 99 | find setUp or tearDown when it links, you'll know you need to at least include |
| 100 | an empty function for these. |
| 101 | |
| 102 | The majority of the file will be a series of test functions. Test functions |
| 103 | follow the convention of starting with the word "test" or "spec". You don't HAVE |
| 104 | to name them this way, but it makes it clear what functions are tests for other |
| 105 | developers. Test functions take no arguments and return nothing. All test |
| 106 | accounting is handled internally in Unity. |
| 107 | |
| 108 | Finally, at the bottom of your test file, you will write a `main()` function. |
| 109 | This function will call `UNITY_BEGIN()`, then `RUN_TEST` for each test, and |
| 110 | finally `UNITY_END()`.This is what will actually trigger each of those test |
| 111 | functions to run, so it is important that each function gets its own `RUN_TEST` |
| 112 | call. |
| 113 | |
| 114 | Remembering to add each test to the main function can get to be tedious. If you |
| 115 | enjoy using helper scripts in your build process, you might consider making use |
| 116 | of our handy generate_test_runner.rb script. This will create the main function |
| 117 | and all the calls for you, assuming that you have followed the suggested naming |
| 118 | conventions. In this case, there is no need for you to include the main function |
| 119 | in your test file at all. |
| 120 | |
| 121 | When 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 | |
| 127 | void setUp(void) { |
| 128 | // set stuff up here |
| 129 | } |
| 130 | |
| 131 | void tearDown(void) { |
| 132 | // clean stuff up here |
| 133 | } |
| 134 | |
| 135 | void test_function_should_doBlahAndBlah(void) { |
| 136 | //test stuff |
| 137 | } |
| 138 | |
| 139 | void test_function_should_doAlsoDoBlah(void) { |
| 140 | //more test stuff |
| 141 | } |
| 142 | |
| 143 | int 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 | |
| 151 | It's possible that you will require more customization than this, eventually. |
| 152 | For that sort of thing, you're going to want to look at the configuration guide. |
| 153 | This should be enough to get you going, though. |
| 154 | |
| 155 | |
| 156 | ## How to Build and Run A Test File |
| 157 | |
| 158 | This is the single biggest challenge to picking up a new unit testing framework, |
| 159 | at least in a language like C or C++. These languages are REALLY good at getting |
| 160 | you "close to the metal" (why is the phrase metal? Wouldn't it be more accurate |
| 161 | to say "close to the silicon"?). While this feature is usually a good thing, it |
| 162 | can make testing more challenging. |
| 163 | |
| 164 | You have two really good options for toolchains. Depending on where you're |
| 165 | coming from, it might surprise you that neither of these options is running the |
| 166 | unit tests on your hardware. |
| 167 | There 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 | |
| 173 | Instead of running your tests on your actual hardware, most developers choose to |
| 174 | develop them as native applications (using gcc or MSVC for example) or as |
| 175 | applications running on a simulator. Either is a good option. Native apps have |
| 176 | the advantages of being faster and easier to set up. Simulator apps have the |
| 177 | advantage of working with the same compiler as your target application. The |
| 178 | options for configuring these are discussed in the configuration guide. |
| 179 | |
| 180 | To get either to work, you might need to make a few changes to the file |
| 181 | containing your register set (discussed later). |
| 182 | |
| 183 | In either case, a test is built by linking unity, the test file, and the C |
| 184 | file(s) being tested. These files create an executable which can be run as the |
| 185 | test set for that module. Then, this process is repeated for the next test file. |
| 186 | This flexibility of separating tests into individual executables allows us to |
| 187 | much more thoroughly unit test our system and it keeps all the test code out of |
| 188 | our final release! |
| 189 | |
| 190 | |
| 191 | *Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)* |