git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 8/8] t/README: Document the do's and don'ts of tests

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 1, 2010, 20:17 UTC
Message-ID
<1278015478-6920-9-git-send-email-avarab@gmail.com>
In-Reply-To
<1278015478-6920-1-git-send-email-avarab@gmail.com>

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 <7vhbkj2kcr.fsf@alter.siamese.dyndns.org>.

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 335f87871fe5aa6b3fd55b2b4e80f16fe9681483 when TAP support was introduced.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/README |   78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)
diff --git a/t/README b/t/README
index 5a291e1..4a1cfc9 100644
--- a/t/README
+++ b/t/README
@@ -231,6 +231,84 @@ This test harness library does the following things:
    consistently when command line arguments --verbose (or -v),
    --debug (or -d), and --immediate (or -i) is given.
 
+Do's, don'ts & things to keep in mind
+-------------------------------------
+
+Here's a few examples of things you probably should and shouldn't do
+when writing tests.
+
+Do:
+
+ - Put as much code as possible inside test_expect_success and other
+   assertions.
+
+   Even code that isn't a test per se, but merely some setup code
+   should be inside a test assertion if at all possible. Test scripts
+   should only have trivial code outside of their assertions.
+
+ - Chain your test assertions
+
+   Write test code like this:
+
+	git merge foo &&
+	git push bar &&
+	test ...
+
+   Instead of:
+
+	git merge hla
+	git push gh
+	test ...
+
+   That way all of the commands in your tests will succeed or fail. If
+   you must ignore the return value of something (e.g. the return
+   value of export is unportable) it's best to indicate so explicitly
+   with a semicolon:
+
+	export HLAGH;
+	git merge hla &&
+	git push gh &&
+	test ...
+
+Don't:
+
+ - exit() within a <script> part.
+
+   The harness will catch this as a programming error of the test.
+   Use test_done instead if you need to stop the tests early (see
+   "Skipping tests" below).
+
+ - Break the TAP output
+
+   The raw output from your test might be interpreted by a TAP
+   harness. You usually don't have to worry about that. TAP harnesses
+   will ignore everything they don't know about, but don't step on
+   their toes in these areas:
+
+   - Don't print lines like "$x..$y" where $x and $y are integers.
+
+   - Don't print lines that begin with "ok" or "not ok".
+
+   A TAP harness expect a line that begins with either "ok" and "not
+   ok" to signal a test passed or failed (and our harness already
+   produces such lines), so your script shouldn't emit such lines to
+   their output.
+
+   You can gleam some further possible issues from the TAP grammar
+   (see http://search.cpan.org/perldoc?TAP::Parser::Grammar#TAP_Grammar)
+   but the best indication is to just run the tests with prove(1),
+   it'll complain if anything is amiss.
+
+Keep in mind:
+
+ - That what you print to stderr and stdout is usually ignored
+
+   Inside <script> part, the standard output and standard error
+   streams are discarded, and the test harness only reports "ok" or
+   "not ok" to the end user running the tests. Under --verbose, they
+   are shown to help debugging the tests.
+
+
 Skipping tests
 --------------
 
-- 
1.7.1.251.g92a7
Previous: Ævar Arnfjörð BjarmasonNext: Jeff King
Message 13 of 14 in “Improvements for t/README”
  1. 0/8 Improvements for t/READMEÆvar Arnfjörð Bjarmason, Jul 1, 2010
  2. 1/8 t/README: Tests are all +x, ./test, not sh ./testÆvar Arnfjörð Bjarmason, Jul 1, 2010
  3. Junio C HamanoJul 1, 2010
  4. Ævar Arnfjörð BjarmasonJul 2, 2010
  5. 2/8 t/README: The trash is in 't/trash directory.$name'Ævar Arnfjörð Bjarmason, Jul 1, 2010
  6. 3/8 t/README: Typo: paralell -> parallelÆvar Arnfjörð Bjarmason, Jul 1, 2010
  7. 4/8 t/README: Document the prereq functions, and 3-arg test_*Ævar Arnfjörð Bjarmason, Jul 1, 2010
  8. 5/8 t/README: Document test_external*Ævar Arnfjörð Bjarmason, Jul 1, 2010
  9. Jakub NarebskiJul 1, 2010
  10. 5/8 t/README: Document test_external*Ævar Arnfjörð Bjarmason, Jul 1, 2010
  11. 6/8 t/README: Document test_expect_codeÆvar Arnfjörð Bjarmason, Jul 1, 2010
  12. 7/8 t/README: Add a section about skipping testsÆvar Arnfjörð Bjarmason, Jul 1, 2010
  13. 8/8 t/README: Document the do's and don'ts of testsÆvar Arnfjörð Bjarmason, Jul 1, 2010
  14. Jeff KingJul 2, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.