Skip to content

Commit 20873f4

Browse files
avargitster
authored andcommitted
t/README: Document the do's and don'ts of tests
Add a "Do's, don'ts & things to keep in mind" subsection to the "Writing Tests" documentation. Much of this is based on Junio C Hamano's "Test your stuff" section in <[email protected]>. I turned it into a list of do's and don'ts to make it easier to skim it, and integrated my note that a TAP harness will get confused if you print "ok" or "not ok" at the beginning of a line. Thad had to be fixed in 335f878 when TAP support was introduced. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5500d1 commit 20873f4

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

t/README

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,84 @@ This test harness library does the following things:
231231
consistently when command line arguments --verbose (or -v),
232232
--debug (or -d), and --immediate (or -i) is given.
233233

234+
Do's, don'ts & things to keep in mind
235+
-------------------------------------
236+
237+
Here's a few examples of things you probably should and shouldn't do
238+
when writing tests.
239+
240+
Do:
241+
242+
- Put as much code as possible inside test_expect_success and other
243+
assertions.
244+
245+
Even code that isn't a test per se, but merely some setup code
246+
should be inside a test assertion if at all possible. Test scripts
247+
should only have trivial code outside of their assertions.
248+
249+
- Chain your test assertions
250+
251+
Write test code like this:
252+
253+
git merge foo &&
254+
git push bar &&
255+
test ...
256+
257+
Instead of:
258+
259+
git merge hla
260+
git push gh
261+
test ...
262+
263+
That way all of the commands in your tests will succeed or fail. If
264+
you must ignore the return value of something (e.g. the return
265+
value of export is unportable) it's best to indicate so explicitly
266+
with a semicolon:
267+
268+
export HLAGH;
269+
git merge hla &&
270+
git push gh &&
271+
test ...
272+
273+
Don't:
274+
275+
- exit() within a <script> part.
276+
277+
The harness will catch this as a programming error of the test.
278+
Use test_done instead if you need to stop the tests early (see
279+
"Skipping tests" below).
280+
281+
- Break the TAP output
282+
283+
The raw output from your test might be interpreted by a TAP
284+
harness. You usually don't have to worry about that. TAP harnesses
285+
will ignore everything they don't know about, but don't step on
286+
their toes in these areas:
287+
288+
- Don't print lines like "$x..$y" where $x and $y are integers.
289+
290+
- Don't print lines that begin with "ok" or "not ok".
291+
292+
A TAP harness expect a line that begins with either "ok" and "not
293+
ok" to signal a test passed or failed (and our harness already
294+
produces such lines), so your script shouldn't emit such lines to
295+
their output.
296+
297+
You can glean some further possible issues from the TAP grammar
298+
(see http://search.cpan.org/perldoc?TAP::Parser::Grammar#TAP_Grammar)
299+
but the best indication is to just run the tests with prove(1),
300+
it'll complain if anything is amiss.
301+
302+
Keep in mind:
303+
304+
- That what you print to stderr and stdout is usually ignored
305+
306+
Inside <script> part, the standard output and standard error
307+
streams are discarded, and the test harness only reports "ok" or
308+
"not ok" to the end user running the tests. Under --verbose, they
309+
are shown to help debugging the tests.
310+
311+
234312
Skipping tests
235313
--------------
236314

0 commit comments

Comments
 (0)