Debian Codegraph @

vendor/unity/docs/UnityHelperScriptsGuide.md

raw
1 # Unity Helper Scripts
2
3 ## With a Little Help From Our Friends
4
5 Sometimes what it takes to be a really efficient C programmer is a little non-C.
6 The Unity project includes a couple of Ruby scripts for making your life just a tad easier.
7 They are completely optional.
8 If you choose to use them, you'll need a copy of Ruby, of course.
9 Just install whatever the latest version is, and it is likely to work. You can find Ruby at [ruby-lang.org][].
10
11 ### `generate_test_runner.rb`
12
13 Are you tired of creating your own `main` function in your test file?
14 Do you keep forgetting to add a `RUN_TEST` call when you add a new test case to your suite?
15 Do you want to use CMock or other fancy add-ons but don't want to figure out how to create your own `RUN_TEST` macro?
16
17 Well then we have the perfect script for you!
18
19 The `generate_test_runner` script processes a given test file and automatically creates a separate test runner file that includes ?main?to execute the test cases within the scanned test file.
20 All you do then is add the generated runner to your list of files to be compiled and linked, and presto you're done!
21
22 This script searches your test file for void function signatures having a function name beginning with "test" or "spec".
23 It treats each of these functions as a test case and builds up a test suite of them.
24 For example, the following includes three test cases:
25
26 ```C
27 void testVerifyThatUnityIsAwesomeAndWillMakeYourLifeEasier(void)
28 {
29 ASSERT_TRUE(1);
30 }
31 void test_FunctionName_should_WorkProperlyAndReturn8(void) {
32 ASSERT_EQUAL_INT(8, FunctionName());
33 }
34 void spec_Function_should_DoWhatItIsSupposedToDo(void) {
35 ASSERT_NOT_NULL(Function(5));
36 }
37 ```
38
39 You can run this script a couple of ways.
40 The first is from the command line:
41
42 ```Shell
43 ruby generate_test_runner.rb TestFile.c NameOfRunner.c
44 ```
45
46 Alternatively, if you include only the test file parameter, the script will copy the name of the test file and automatically append `_Runner` to the name of the generated file.
47 The example immediately below will create TestFile_Runner.c.
48
49 ```Shell
50 ruby generate_test_runner.rb TestFile.c
51 ```
52
53 You can also add a [YAML][] file to configure extra options.
54 Conveniently, this YAML file is of the same format as that used by Unity and CMock.
55 So if you are using YAML files already, you can simply pass the very same file into the generator script.
56
57 ```Shell
58 ruby generate_test_runner.rb TestFile.c my_config.yml
59 ```
60
61 The contents of the YAML file `my_config.yml` could look something like the example below.
62 If you're wondering what some of these options do, you're going to love the next section of this document.
63
64 ```YAML
65 :unity:
66 :includes:
67 - stdio.h
68 - microdefs.h
69 :cexception: 1
70 :suite_setup: "blah = malloc(1024);"
71 :suite_teardown: "free(blah);"
72 ```
73
74 If you would like to force your generated test runner to include one or more header files, you can just include those at the command line too.
75 Just make sure these are _after_ the YAML file, if you are using one:
76
77 ```Shell
78 ruby generate_test_runner.rb TestFile.c my_config.yml extras.h
79 ```
80
81 Another option, particularly if you are already using Ruby to orchestrate your builds - or more likely the Ruby-based build tool Rake - is requiring this script directly.
82 Anything that you would have specified in a YAML file can be passed to the script as part of a hash.
83 Let's push the exact same requirement set as we did above but this time through Ruby code directly:
84
85 ```Ruby
86 require "generate_test_runner.rb"
87 options = {
88 :includes => ["stdio.h", "microdefs.h"],
89 :cexception => 1,
90 :suite_setup => "blah = malloc(1024);",
91 :suite_teardown => "free(blah);"
92 }
93 UnityTestRunnerGenerator.new.run(testfile, runner_name, options)
94 ```
95
96 If you have multiple files to generate in a build script (such as a Rakefile), you might want to instantiate a generator object with your options and call it to generate each runner afterwards.
97 Like thus:
98
99 ```Ruby
100 gen = UnityTestRunnerGenerator.new(options)
101 test_files.each do |f|
102 gen.run(f, File.basename(f,'.c')+"Runner.c"
103 end
104 ```
105
106 #### Options accepted by generate_test_runner.rb
107
108 The following options are available when executing `generate_test_runner`.
109 You may pass these as a Ruby hash directly or specify them in a YAML file, both of which are described above.
110 In the `examples` directory, Example 3's Rakefile demonstrates using a Ruby hash.
111
112 ##### `:includes`
113
114 This option specifies an array of file names to be `#include`'d at the top of your runner C file.
115 You might use it to reference custom types or anything else universally needed in your generated runners.
116
117 ##### `:suite_setup`
118
119 Define this option with C code to be executed _before any_ test cases are run.
120
121 Alternatively, if your C compiler supports weak symbols, you can leave this option unset and instead provide a `void suiteSetUp(void)` function in your test suite.
122 The linker will look for this symbol and fall back to a Unity-provided stub if it is not found.
123
124 ##### `:suite_teardown`
125
126 Define this option with C code to be executed _after all_ test cases have finished.
127 An integer variable `num_failures` is available for diagnostics.
128 The code should end with a `return` statement; the value returned will become the exit code of `main`.
129 You can normally just return `num_failures`.
130
131 Alternatively, if your C compiler supports weak symbols, you can leave this option unset and instead provide a `int suiteTearDown(int num_failures)` function in your test suite.
132 The linker will look for this symbol and fall back to a Unity-provided stub if it is not found.
133
134 ##### `:enforce_strict_ordering`
135
136 This option should be defined if you have the strict order feature enabled in CMock (see CMock documentation).
137 This generates extra variables required for everything to run smoothly.
138 If you provide the same YAML to the generator as used in CMock's configuration, you've already configured the generator properly.
139
140 ##### `:externc`
141
142 This option should be defined if you are mixing C and CPP and want your test runners to automatically include extern "C" support when they are generated.
143
144 ##### `:mock_prefix` and `:mock_suffix`
145
146 Unity automatically generates calls to Init, Verify and Destroy for every file included in the main test file that starts with the given mock prefix and ends with the given mock suffix, file extension not included.
147 By default, Unity assumes a `Mock` prefix and no suffix.
148
149 ##### `:plugins`
150
151 This option specifies an array of plugins to be used (of course, the array can contain only a single plugin).
152 This is your opportunity to enable support for CException support, which will add a check for unhandled exceptions in each test, reporting a failure if one is detected.
153 To enable this feature using Ruby:
154
155 ```Ruby
156 :plugins => [ :cexception ]
157 ```
158
159 Or as a yaml file:
160
161 ```YAML
162 :plugins:
163 -:cexception
164 ```
165
166 If you are using CMock, it is very likely that you are already passing an array of plugins to CMock.
167 You can just use the same array here.
168 This script will just ignore the plugins that don't require additional support.
169
170 ##### `:include_extensions`
171
172 This option specifies the pattern for matching acceptable header file extensions.
173 By default it will accept hpp, hh, H, and h files.
174 If you need a different combination of files to search, update this from the default `'(?:hpp|hh|H|h)'`.
175
176 ##### `:source_extensions`
177
178 This option specifies the pattern for matching acceptable source file extensions.
179 By default it will accept cpp, cc, C, c, and ino files.
180 If you need a different combination of files to search, update this from the default `'(?:cpp|cc|ino|C|c)'`.
181
182 ### `unity_test_summary.rb`
183
184 A Unity test file contains one or more test case functions.
185 Each test case can pass, fail, or be ignored.
186 Each test file is run individually producing results for its collection of test cases.
187 A given project will almost certainly be composed of multiple test files.
188 Therefore, the suite of tests is comprised of one or more test cases spread across one or more test files.
189 This script aggregates individual test file results to generate a summary of all executed test cases.
190 The output includes how many tests were run, how many were ignored, and how many failed. In addition, the output includes a listing of which specific tests were ignored and failed.
191 A good example of the breadth and details of these results can be found in the `examples` directory.
192 Intentionally ignored and failing tests in this project generate corresponding entries in the summary report.
193
194 If you're interested in other (prettier?) output formats, check into the [Ceedling][] build tool project that works with Unity and CMock and supports xunit-style xml as well as other goodies.
195
196 This script assumes the existence of files ending with the extensions `.testpass` and `.testfail`.
197 The contents of these files includes the test results summary corresponding to each test file executed with the extension set according to the presence or absence of failures for that test file.
198 The script searches a specified path for these files, opens each one it finds, parses the results, and aggregates and prints a summary.
199 Calling it from the command line looks like this:
200
201 ```Shell
202 ruby unity_test_summary.rb build/test/
203 ```
204
205 You can optionally specify a root path as well.
206 This is really helpful when you are using relative paths in your tools' setup, but you want to pull the summary into an IDE like Eclipse for clickable shortcuts.
207
208 ```Shell
209 ruby unity_test_summary.rb build/test/ ~/projects/myproject/
210 ```
211
212 Or, if you're more of a Windows sort of person:
213
214 ```Shell
215 ruby unity_test_summary.rb build\teat\ C:\projects\myproject\
216 ```
217
218 When configured correctly, you'll see a final summary, like so:
219
220 ```Shell
221 --------------------------
222 UNITY IGNORED TEST SUMMARY
223 --------------------------
224 blah.c:22:test_sandwiches_should_HaveBreadOnTwoSides:IGNORE
225
226 -------------------------
227 UNITY FAILED TEST SUMMARY
228 -------------------------
229 blah.c:87:test_sandwiches_should_HaveCondiments:FAIL:Expected 1 was 0
230 meh.c:38:test_soda_should_BeCalledPop:FAIL:Expected "pop" was "coke"
231
232 --------------------------
233 OVERALL UNITY TEST SUMMARY
234 --------------------------
235 45 TOTAL TESTS 2 TOTAL FAILURES 1 IGNORED
236 ```
237
238 How convenient is that?
239
240 *Find The Latest of This And More at [ThrowTheSwitch.org][]*
241
242 [ruby-lang.org]: https://ruby-labg.org/
243 [YAML]: http://www.yaml.org/
244 [Ceedling]: http://www.throwtheswitch.org/ceedling
245 [ThrowTheSwitch.org]: https://throwtheswitch.org