You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The function plan is used to indicate the plan of your test run. Usually you will just give it the number of tests as argument.
@@ -64,9 +64,9 @@ Alternatively, you can give it the skip_all or no_plan constants as arguments. T
64
64
65
65
#### done_testing()
66
66
67
-
```
68
-
void done_testing();
69
-
void done_testing(int number_of_tests);
67
+
```cpp
68
+
void done_testing();
69
+
void done_testing(int number_of_tests);
70
70
```
71
71
72
72
If you don't know how many tests you're going to run, you can issue the plan when you're done running tests.
@@ -78,17 +78,17 @@ This is safer than and replaces the "no_plan" plan.
78
78
By convention, each test is assigned a number in order. This is largely done automatically for you. However, it's often very useful to assign a name to each test. Which would you rather see:
79
79
80
80
```
81
-
ok 4
82
-
not ok 5
83
-
ok 6
81
+
ok 4
82
+
not ok 5
83
+
ok 6
84
84
```
85
85
86
86
or
87
87
88
88
```
89
-
ok 4 - basic multi-variable
90
-
not ok 5 - simple exponential
91
-
ok 6 - force == mass * acceleration
89
+
ok 4 - basic multi-variable
90
+
not ok 5 - simple exponential
91
+
ok 6 - force == mass * acceleration
92
92
```
93
93
94
94
The later gives you some idea of what failed. It also makes it easier to find the test in your script, simply search for "simple exponential".
@@ -101,59 +101,61 @@ All of the following print "ok" or "not ok" depending on if the test succeeded o
ok is the basic test expression in TAP. It simply evaluates any expression, for example, got == expected, taking a true value to mean that the test passed and a false value to mean that the test failed.
109
109
test_name is a very short description of the test that will be printed out. It makes it very easy to find a test in your script when it fails and gives others an idea of your intentions. test_name is optional, but we very strongly encourage its use.
Similar to ok(), is() and isnt() compare their two arguments with == and != respectively and use the result of that to determine if the test succeeded or failed. So these:
119
121
120
-
```
121
-
# Is the ultimate answer 42?
122
-
is(ultimate_answer(), 42, "Meaning of Life");
122
+
```cpp
123
+
# Is the ultimate answer 42?
124
+
is(ultimate_answer(), 42, "Meaning of Life");
123
125
124
-
# foo isn't empty
125
-
isnt(foo, "", "Got some foo");
126
+
# foo isn't empty
127
+
isnt(foo, "", "Got some foo");
126
128
```
127
129
128
130
are similar to these:
129
131
130
-
```
131
-
ok(ultimate_answer() == 42, "Meaning of Life");
132
-
ok(foo != "", "Got some foo");
132
+
```cpp
133
+
ok(ultimate_answer() == 42, "Meaning of Life");
134
+
ok(foo != "", "Got some foo");
133
135
```
134
136
135
137
(Mnemonic: "This is that." "This isn't that.")
136
138
So why use these? They produce better diagnostics on failure. ok() cannot know what you are testing for (beyond the name), but is() and isnt() know what the test was and why it failed. For example this test:
137
139
138
-
```
139
-
std::string foo("waffle"), bar("yarblokos");
140
-
is(foo, bar, 'Is foo the same as bar?' );
140
+
```cpp
141
+
std::string foo("waffle"), bar("yarblokos");
142
+
is(foo, bar, "Is foo the same as bar?");
141
143
```
142
144
143
145
Will produce something like this:
144
146
145
147
```
146
-
not ok 17 - Is foo the same as bar?
147
-
# Failed test 'Is foo the same as bar?'
148
-
# got: 'waffle'
149
-
# expected: 'yarblokos'
148
+
not ok 17 - Is foo the same as bar?
149
+
# Failed test 'Is foo the same as bar?'
150
+
# got: 'waffle'
151
+
# expected: 'yarblokos'
150
152
```
151
153
152
154
#### pass(); fail()
153
155
154
-
```
155
-
bool pass(const std::string& test_name = "");
156
-
bool fail(const std::string& test_name = "");
156
+
```cpp
157
+
bool pass(const std::string& test_name = "");
158
+
bool fail(const std::string& test_name = "");
157
159
```
158
160
159
161
Sometimes you just want to say that the tests have passed. Usually the case is you've got some complicated condition that is difficult to wedge into an ok(). In this case, you can simply use pass() (to declare the test ok) or fail (for not ok). They are synonyms for ok(true, test_name) and ok(false, test_name).
@@ -163,8 +165,8 @@ Use these very, very, very sparingly.
skip tells the TAP harness that you're skipping a number of tests for the given reason. Note that you have to do the skipping yourself.
@@ -175,25 +177,25 @@ If you pick the right test function, you'll usually get a good idea of what went
175
177
176
178
#### diag
177
179
178
-
```
179
-
diag(diagnostic_message...);
180
+
```cpp
181
+
diag(diagnostic_message...);
180
182
```
181
183
182
184
Prints a diagnostic message which is guaranteed not to interfere with test output. The arguments are simply concatenated together.
183
185
Returns false, so as to preserve failure.
184
186
Handy for this sort of thing:
185
187
186
-
```
187
-
ok(has_user("foo"), "There's a foo user") or
188
-
diag("Since there's no foo, check that /etc/bar is set up right");
188
+
```cpp
189
+
ok(has_user("foo"), "There's a foo user") or
190
+
diag("Since there's no foo, check that /etc/bar is set up right");
189
191
```
190
192
191
193
which would produce:
192
194
193
195
```
194
-
not ok 42 - There's a foo user
195
-
# Failed test 'There's a foo user'
196
-
# Since there's no foo, check that /etc/bar is set up right.
196
+
not ok 42 - There's a foo user
197
+
# Failed test 'There's a foo user'
198
+
# Since there's no foo, check that /etc/bar is set up right.
197
199
```
198
200
199
201
You might remember ok() or diag() with the mnemonic open() or die().
@@ -202,24 +204,24 @@ You might remember ok() or diag() with the mnemonic open() or die().
202
204
203
205
#### note
204
206
205
-
```
206
-
note(diagnostic_message...);
207
+
```cpp
208
+
note(diagnostic_message...);
207
209
```
208
210
209
211
Like diag(), except the message will not be seen when the test is run in a harness. It will only be visible in the verbose TAP stream.
210
212
Handy for putting in notes which might be useful for debugging, but don't indicate a problem.
211
213
212
-
```
213
-
note("Tempfile is ", tempfile);
214
+
```cpp
215
+
note("Tempfile is ", tempfile);
214
216
```
215
217
216
218
diag simply catenates its arguments to the error output, while note prints diagnostics to the TAP stream.
217
219
218
220
#### set_output(); set_error()
219
221
220
-
```
221
-
void set_output(std::ofstream& new_output);
222
-
void set_error(std::ofstream& new_error);
222
+
```cpp
223
+
void set_output(std::ofstream& new_output);
224
+
void set_error(std::ofstream& new_error);
223
225
```
224
226
225
227
These set the filehandle of the TAP stream and the error stream. They default to std::cout and std::cerr, respectively. These can only be set before any output is written to them.
@@ -232,18 +234,18 @@ If all your tests passed, Test::Builder will exit with zero (which is normal). I
232
234
So the exit codes are...
233
235
234
236
```
235
-
0 all tests successful
236
-
255 test died or all passed but wrong # of tests run
237
-
any other number how many failed (including missing or extras)
237
+
0 all tests successful
238
+
255 test died or all passed but wrong # of tests run
239
+
any other number how many failed (including missing or extras)
238
240
```
239
241
240
242
If you fail more than 254 tests, it will be reported as 254.
241
243
242
244
#### bail_out()
243
245
244
-
```
245
-
int exit_status();
246
-
void bail_out(const std::string& reason);
246
+
```cpp
247
+
intexit_status();
248
+
voidbail_out(const std::string& reason);
247
249
```
248
250
249
251
**bail_out** terminates the current test program with exit code 255, indicating to the test harness that all subsequent testing should halt. Typically this is used to indicate that testing cannot continue at all.
0 commit comments