| 1 |
# Running and Writing Tests |
| 2 |
|
| 3 |
Mercurial contains a simple regression test framework that allows both Python |
| 4 |
unit tests and shell-script driven regression tests. |
| 5 |
|
| 6 |
## Running the test suite |
| 7 |
|
| 8 |
To run the tests, do: |
| 9 |
|
| 10 |
$ make tests |
| 11 |
cd tests && ./run-tests.py -j8 |
| 12 |
............................................ |
| 13 |
Ran 44 tests, 0 failed. |
| 14 |
|
| 15 |
This finds all scripts in the tests/ directory named test-* and executes them 8 |
| 16 |
at a time. The scripts can be either unified tests, shell scripts, or Python. |
| 17 |
Each test is run in a temporary directory that is removed when the test is |
| 18 |
complete. |
| 19 |
|
| 20 |
You can also run tests individually: |
| 21 |
|
| 22 |
$ cd tests/ |
| 23 |
$ ./run-tests.py test-pull.t test-undo.t |
| 24 |
.. |
| 25 |
Ran 2 tests, 0 failed. |
| 26 |
|
| 27 |
A test-<x> succeeds if the script returns success and its output matches |
| 28 |
test-<x>.out. If the new output doesn't match, it is stored in test-<x>.err. |
| 29 |
|
| 30 |
Also, 'run-tests.py' has some useful options: |
| 31 |
|
| 32 |
* -i: interactively accept test changes |
| 33 |
* -r: rerun tests with errors |
| 34 |
* -f: exit on first failure |
| 35 |
* -R: restart after last error |
| 36 |
* -j: run multiple threads |
| 37 |
* -l: skip building a private hg install |
| 38 |
* --view: view output differences with an external tool |
| 39 |
|
| 40 |
See 'run-tests.py -h' for a full list. |
| 41 |
|
| 42 |
One option that comes in handy when running tests repeatedly is '--local'. By |
| 43 |
default, 'run-tests.py' installs Mercurial into its temporary directory for each |
| 44 |
run of the test suite. You can save several seconds per run with '--local', |
| 45 |
which tells 'run-tests.py' simply to use the local 'hg' script and library. The |
| 46 |
catch: if you edit the code during a long test suite run, different tests will |
| 47 |
run with different code. It's best to use '--local' when you are running the |
| 48 |
same test script many times, as often happens during development. |
| 49 |
|
| 50 |
## Writing a shell script test |
| 51 |
|
| 52 |
### Be careful with new test scripts! |
| 53 |
|
| 54 |
The test suite is slow. And the test suite is slow because it is highly |
| 55 |
redundant. And it is highly redundant because for years we've been writing a |
| 56 |
completely new test for each issue that creates a new repo, adds a file, runs |
| 57 |
status, commits, does a merge, etc. |
| 58 |
|
| 59 |
If we add a one-second test for each bug fix that shows up, very soon we'll have |
| 60 |
a test suite that takes an hour to run and thus is no longer useful to anyone. |
| 61 |
|
| 62 |
Therefore, if you want to add testing for a feature, you must either: |
| 63 |
|
| 64 |
* add a short, fast doctest (where appropriate) |
| 65 |
* fold your test into an appropriate existing test |
| 66 |
|
| 67 |
When doing the latter, you should try to take advantage of work the test suite |
| 68 |
is already doing. For instance, if you're testing whether uppercase keywords |
| 69 |
work correctly, please adjust one of the many existing tests that uses a keyword |
| 70 |
to use an uppercase one. |
| 71 |
|
| 72 |
If you are adding a small tests for a bugfix/improvement to an existing feature |
| 73 |
please add it to an existing test file related to this feature. Only fallback to |
| 74 |
new test file when you are opening a significant new feature space and you know |
| 75 |
that the test file will gather significant content over time. |
| 76 |
|
| 77 |
Patches that add completely new test file for a trivial case will likely be |
| 78 |
rejected. |
| 79 |
|
| 80 |
### Basic example |
| 81 |
|
| 82 |
Creating a regression test is easy. Simply create a *.t file which contains |
| 83 |
shell script commands prepended with ' $ '. Lines not starting with two spaces |
| 84 |
are comments. |
| 85 |
|
| 86 |
Here's an example (test-x.t): |
| 87 |
|
| 88 |
File replaced with directory: |
| 89 |
|
| 90 |
$ hg init a |
| 91 |
$ cd a |
| 92 |
$ echo a > a |
| 93 |
$ hg commit -Ama |
| 94 |
$ rm a |
| 95 |
$ mkdir a |
| 96 |
$ echo a > a/a |
| 97 |
|
| 98 |
Should fail - would corrupt dirstate: |
| 99 |
|
| 100 |
$ hg add a/a |
| 101 |
|
| 102 |
Then run this test for the first time: |
| 103 |
|
| 104 |
$ python run-tests.py -i test-x.t |
| 105 |
|
| 106 |
ERROR: /home/adi/hgrepos/hg-crew/tests/test-x.t output changed |
| 107 |
--- /home/adi/hgrepos/hg-crew/tests/test-x.t |
| 108 |
+++ /home/adi/hgrepos/hg-crew/tests/test-x.t.err |
| 109 |
@@ -4,6 +4,7 @@ |
| 110 |
$ cd a |
| 111 |
$ echo a > a |
| 112 |
$ hg commit -Ama |
| 113 |
+ adding a |
| 114 |
$ rm a |
| 115 |
$ mkdir a |
| 116 |
$ echo a > a/a |
| 117 |
@@ -11,4 +12,6 @@ |
| 118 |
Should fail - would corrupt dirstate: |
| 119 |
|
| 120 |
$ hg add a/a |
| 121 |
+ abort: file 'a' in dirstate clashes with 'a/a' |
| 122 |
+ [255] |
| 123 |
|
| 124 |
!Accept this change? [n] |
| 125 |
|
| 126 |
Check the output of the commands inserted into your test file and accept the |
| 127 |
modified test file with 'y'. |
| 128 |
|
| 129 |
The test file now includes both command input interspersed with command output: |
| 130 |
|
| 131 |
File replaced with directory: |
| 132 |
|
| 133 |
$ hg init a |
| 134 |
$ cd a |
| 135 |
$ echo a > a |
| 136 |
$ hg commit -Ama |
| 137 |
adding a |
| 138 |
$ rm a |
| 139 |
$ mkdir a |
| 140 |
$ echo a > a/a |
| 141 |
|
| 142 |
Should fail - would corrupt dirstate: |
| 143 |
|
| 144 |
$ hg add a/a |
| 145 |
abort: file 'a' in dirstate clashes with 'a/a' |
| 146 |
[255] |
| 147 |
|
| 148 |
Note how nonzero return values show up enclosed in squared brackets ("[255]" for |
| 149 |
"hg add a/a"). |
| 150 |
|
| 151 |
Running this test again will now pass |
| 152 |
|
| 153 |
$ python run-tests.py test-x.t -i |
| 154 |
. |
| 155 |
# Ran 1 tests, 0 skipped, 0 failed. |
| 156 |
|
| 157 |
This kind of test is also known as "unified test" (because it unifies input and |
| 158 |
output into the same file). |
| 159 |
|
| 160 |
### Filtering output |
| 161 |
|
| 162 |
Such tests must be repeatable, that is, output generated by commands must not |
| 163 |
contain strings that change for each invocation (like the path of a temporary |
| 164 |
file). |
| 165 |
|
| 166 |
To cope with this kind of variation, unified tests support filtering using |
| 167 |
(glob) or (re). |
| 168 |
|
| 169 |
To enable glob filtering for an output line, append " (glob)" to the respective |
| 170 |
line like in the following example: |
| 171 |
|
| 172 |
test version |
| 173 |
$ hg version -q |
| 174 |
Mercurial Distributed SCM (version *) (glob) |
| 175 |
|
| 176 |
(glob) filtering supports * for matching a string and ? for matching a single |
| 177 |
character. Example: |
| 178 |
|
| 179 |
test diff |
| 180 |
$ hg diff |
| 181 |
diff -r ???????????? orphanchild (glob) |
| 182 |
--- /dev/null |
| 183 |
+++ b/orphanchild |
| 184 |
@@ -0,0 +1,1 @@ |
| 185 |
+orphan |
| 186 |
|
| 187 |
Literal * or ? on (glob) lines must be escaped with \ (backslash). |
| 188 |
|
| 189 |
To use regular expression filtering on a line, append " (re)" to the output |
| 190 |
line: |
| 191 |
|
| 192 |
test version |
| 193 |
$ hg version -q |
| 194 |
Mercurial Distributed SCM \(version .*\) (re) |
| 195 |
|
| 196 |
Entire lines can be marked optional with " (?)": |
| 197 |
|
| 198 |
test status |
| 199 |
$ hg status |
| 200 |
A new/test/file.txt |
| 201 |
M random/logs/garbage.log (?) |
| 202 |
|
| 203 |
### Inline Python |
| 204 |
|
| 205 |
It is possible to add snippets of Python into tests where convenient: |
| 206 |
|
| 207 |
Create a files with various characters: |
| 208 |
|
| 209 |
>>> a = open('a', 'wb') |
| 210 |
>>> for x in xrange(256): |
| 211 |
... a.write(ord(x)) |
| 212 |
$ hg add a |
| 213 |
|
| 214 |
### Format summary |
| 215 |
|
| 216 |
The format in a nutshell |
| 217 |
|
| 218 |
* Unified tests use the .t file extension. |
| 219 |
* Lines beginning with two spaces, a dollar sign, and a space are run in the |
| 220 |
shell. |
| 221 |
* Lines beginning with two spaces, a greater than sign, and a space allow |
| 222 |
multi-line commands. |
| 223 |
* Lines beginning with ' >>>' are Python code. |
| 224 |
* All other lines beginning with two spaces are considered command output. |
| 225 |
* Output lines ending with a space and the keyword (re) are matched as |
| 226 |
Perl-compatible regular expressions. |
| 227 |
* Output lines ending with a space and the keyword (glob) are matched with a |
| 228 |
glob-like syntax. The only special characters supported are "*" and "?". Both |
| 229 |
characters can be escaped using "\", and the backslash can be escaped itself. |
| 230 |
* Output lines ending with either of the above keywords are always first matched |
| 231 |
literally with actual command output. |
| 232 |
* Output lines ending with a space and the keyword (?) are considered optional. |
| 233 |
This keyword may be combined with (glob) or (re) noted above. |
| 234 |
|
| 235 |
Anything else is a comment. |
| 236 |
|
| 237 |
### Documenting tests |
| 238 |
|
| 239 |
Commenting your tests is at least as important as commenting code. You should |
| 240 |
add documentation to ensure that: |
| 241 |
|
| 242 |
* The behavior that the test intends to test is clear. |
| 243 |
* Any part of the setup important to test that behavior is highlighted. |
| 244 |
* Which behavior is intentionally tested and enforced. |
| 245 |
* Boundaries between individual test cases are clearly identified. |
| 246 |
* A test file should document what it intends on covering. |
| 247 |
|
| 248 |
Documenting a test is done using a reStructuredText like syntax. Example |
| 249 |
`test-foo-bar.t` content: |
| 250 |
|
| 251 |
=============================== |
| 252 |
Testing Foo and Bar interaction |
| 253 |
=============================== |
| 254 |
|
| 255 |
This file gathers test cases that check how the Foo feature interacts with |
| 256 |
operations related to Bar. |
| 257 |
|
| 258 |
Setup |
| 259 |
===== |
| 260 |
|
| 261 |
$ cat << EOF >> $HGRCPATH |
| 262 |
> [bar] |
| 263 |
> some-config = "babar" |
| 264 |
> EOF |
| 265 |
|
| 266 |
[…] |
| 267 |
|
| 268 |
Testing read-only operations |
| 269 |
============================ |
| 270 |
|
| 271 |
Reading an empty Foo |
| 272 |
-------------------- |
| 273 |
|
| 274 |
Operating on a blue Bar without a Foo |
| 275 |
|
| 276 |
$ … |
| 277 |
|
| 278 |
Output should remain empty |
| 279 |
|
| 280 |
$ hg foo --rev bar-blu […] |
| 281 |
|
| 282 |
|
| 283 |
Reading a simple Foo |
| 284 |
-------------------- |
| 285 |
|
| 286 |
[…] |
| 287 |
|
| 288 |
Testing read-write operations |
| 289 |
============================= |
| 290 |
|
| 291 |
[…] |
| 292 |
|
| 293 |
|
| 294 |
### Making tests repeatable |
| 295 |
|
| 296 |
There are some tricky points here that you should be aware of when writing |
| 297 |
tests: |
| 298 |
|
| 299 |
* hg commit wants user interaction - use -m "text" |
| 300 |
* hg up -m wants user interaction, set HGMERGE to something noninteractive: |
| 301 |
|
| 302 |
#!/bin/sh |
| 303 |
cat <<EOF > merge |
| 304 |
echo merging for `basename $1` |
| 305 |
EOF |
| 306 |
chmod +x merge |
| 307 |
env HGMERGE=./merge hg update -m 1 |
| 308 |
|
| 309 |
### Making tests portable |
| 310 |
|
| 311 |
{i} Most of these issues are caught by 'contrib/check-code.py' |
| 312 |
|
| 313 |
You also need to be careful that the tests are portable from one platform to |
| 314 |
another. You're probably working on Linux, where the GNU toolchain has more (or |
| 315 |
different) functionality than on MacOS, *BSD, Solaris, AIX, etc. While testing |
| 316 |
on all platforms is the only sure-fire way to make sure that you've written |
| 317 |
portable code, here's a list of problems that have been found and fixed in the |
| 318 |
tests. Another, more comprehensive list may be found in the GNU Autoconf manual. |
| 319 |
|
| 320 |
#### sh |
| 321 |
|
| 322 |
The Bourne shell is a very basic shell. On Linux, /bin/sh is typically bash, |
| 323 |
which even in Bourne-shell mode has many features that Bourne shells on other |
| 324 |
Unix systems don't have. (Note however that on Linux /bin/sh isn't guaranteed to |
| 325 |
be bash; in particular, on Ubuntu, /bin/sh is dash, a small Posix-compliant |
| 326 |
shell that lacks many bash features). You'll need to be careful about constructs |
| 327 |
that seem ubiquitous, but are actually not available in the least common |
| 328 |
denominator. While using another shell (ksh, bash explicitly, posix shell, etc.) |
| 329 |
explicitly may seem like another option, these may not exist in a portable |
| 330 |
location, and so are generally probably not a good idea. You may find that |
| 331 |
rewriting the test in python will be easier. |
| 332 |
|
| 333 |
* don't use pushd/popd; save the output of "pwd" and use "cd" in place of the |
| 334 |
pushd, and cd back to the saved pwd instead of popd. |
| 335 |
* don't use math expressions like let, (( ... )), or $(( ... )); use "expr" |
| 336 |
instead. |
| 337 |
* don't use $(...) command substitution; use `...` instead. |
| 338 |
* don't use $PWD; use `pwd` instead. |
| 339 |
* don't use $RANDOM; either use inline python or don't rely on random values at |
| 340 |
all. |
| 341 |
* don't use the "function" keyword to define functions; use the old-style form |
| 342 |
instead: |
| 343 |
|
| 344 |
# DON'T USE THIS |
| 345 |
function foo { |
| 346 |
... |
| 347 |
} |
| 348 |
|
| 349 |
# USE THIS INSTEAD |
| 350 |
foo () { |
| 351 |
... |
| 352 |
} |
| 353 |
* don't use "source" to load another script; use "." instead. |
| 354 |
|
| 355 |
#### grep |
| 356 |
|
| 357 |
* don't use the -q option; redirect stdout to /dev/null instead. |
| 358 |
* don't use the -a option; use inline python (-a is not on Solaris). |
| 359 |
* don't use extended regular expressions with grep; use egrep instead, and don't |
| 360 |
escape any regex operators. |
| 361 |
* don't use \S in regular expressions (BSD egrep does not like it). |
| 362 |
* don't use context flags -A, -B or -C (they're not on Solaris). |
| 363 |
|
| 364 |
#### sed |
| 365 |
|
| 366 |
* try to use test globs and regexes instead |
| 367 |
* make sure that the beginning-of-line matcher ("^") is at the very beginning of |
| 368 |
the expression -- it may not be supported inside parens. |
| 369 |
* don't use the -i option; instead, redirect to a file: |
| 370 |
|
| 371 |
sed -e 's/foo/bar/' a > a.new |
| 372 |
mv a.new a |
| 373 |
|
| 374 |
* "i" (and maybe some other functions) requires back-slash ("\\") and new-lines |
| 375 |
on both side of text to insert line on some platforms(e.g.: Mac OS X and |
| 376 |
recent Solaris, at least) without GNU sed |
| 377 |
|
| 378 |
# insert new "foo bar" line before existing 2nd line in target |
| 379 |
$ sed -e '2i\ |
| 380 |
> foo bar |
| 381 |
> ' target |
| 382 |
$ |
| 383 |
|
| 384 |
#### echo |
| 385 |
|
| 386 |
* echo may interpret "\n" and print a newline; use printf instead if you want a |
| 387 |
literal "\n" (backslash + n). |
| 388 |
|
| 389 |
#### false |
| 390 |
|
| 391 |
* false is guaranteed only to return a non-zero value; you cannot depend on it |
| 392 |
being 1. On Solaris in particular, /bin/false returns 255. Rewrite your test |
| 393 |
to not depend on a particular return value, or create a temporary "false" |
| 394 |
executable, and call that instead. |
| 395 |
|
| 396 |
#### diff |
| 397 |
|
| 398 |
* don't use the -N option. There's no particularly good workaround short of |
| 399 |
writing a reasonably complicated replacement script, but substituting gdiff |
| 400 |
for diff if you can't rewrite the test not to need -N will probably do. |
| 401 |
* before using the -u or -U option compare files with cmp (on Solaris diff -u/-U |
| 402 |
isn't silent when the files are identical). |
| 403 |
|
| 404 |
#### wc |
| 405 |
|
| 406 |
* don't use it, or else eliminate leading whitespace from the output with test |
| 407 |
globs |
| 408 |
|
| 409 |
#### head |
| 410 |
|
| 411 |
* don't use the -c option (not part of SUSv3, not supported on OpenBSD). |
| 412 |
Instead, use dd. the following are equivalent; the latter is preferred: |
| 413 |
|
| 414 |
head -c 20 foo > bar |
| 415 |
|
| 416 |
dd if=foo of=bar bs=1 count=20 2>/dev/null |
| 417 |
|
| 418 |
#### ls |
| 419 |
|
| 420 |
* don't use the -R option. Instead, use find(1). |
| 421 |
* make sure options are put before file names. |
| 422 |
|
| 423 |
#### tr |
| 424 |
|
| 425 |
* don't use ranges like tr a-z A-Z . Classes like tr [:lower:] [:upper:] can be |
| 426 |
used instead. |
| 427 |
|
| 428 |
### A naming scheme for test elements |
| 429 |
|
| 430 |
Rather than use an ad-hoc mix of names like foo, bar, baz for generic names in |
| 431 |
tests, consider the following scheme when writing new test cases: |
| 432 |
|
| 433 |
* 0, 1, 2, 3... for commit messages (each commit message matches its expected |
| 434 |
revision) |
| 435 |
* f1, f2, f3... for generic filenames |
| 436 |
* c1, c2, c3... for generic file contents (easily identifiable in the output) |
| 437 |
* d1, d2, d3... for generic directory names |
| 438 |
* r for repos, t for tags, b for branches, u for users, and so on |
| 439 |
|
| 440 |
If you've only got one directory, one file, etc. in your test, you can drop the |
| 441 |
'1'. |
| 442 |
|
| 443 |
## Writing a Python unit test |
| 444 |
|
| 445 |
A unit test operates much like a regression test, but is written in Python. |
| 446 |
Here's an example: |
| 447 |
|
| 448 |
#test-foo.py |
| 449 |
1 #!/usr/bin/env python |
| 450 |
2 |
| 451 |
3 import sys |
| 452 |
4 from mercurial import bdiff, mpatch |
| 453 |
5 |
| 454 |
6 def test1(a, b): |
| 455 |
7 d = bdiff.bdiff(a, b) |
| 456 |
8 c = a |
| 457 |
9 if d: |
| 458 |
10 c = mpatch.patches(a, [d]) |
| 459 |
11 if c != b: |
| 460 |
12 print "***", `a`, `b` |
| 461 |
13 print "bad:" |
| 462 |
14 print `c`[:200] |
| 463 |
15 print `d` |
| 464 |
16 |
| 465 |
17 def test(a, b): |
| 466 |
18 print "***", `a`, `b` |
| 467 |
19 test1(a, b) |
| 468 |
20 test1(b, a) |
| 469 |
21 |
| 470 |
22 test("a\nc\n\n\n\n", "a\nb\n\n\n") |
| 471 |
23 test("a\nb\nc\n", "a\nc\n") |
| 472 |
24 test("", "") |
| 473 |
25 test("a\nb\nc", "a\nb\nc") |
| 474 |
26 test("a\nb\nc\nd\n", "a\nd\n") |
| 475 |
27 test("a\nb\nc\nd\n", "a\nc\ne\n") |
| 476 |
28 test("a\nb\nc\n", "a\nc\n") |
| 477 |
29 test("a\n", "c\na\nb\n") |
| 478 |
30 test("a\n", "") |
| 479 |
31 test("a\n", "b\nc\n") |
| 480 |
32 test("a\n", "c\na\n") |
| 481 |
33 test("", "adjfkjdjksdhfksj") |
| 482 |
34 test("", "ab") |
| 483 |
35 test("", "abc") |
| 484 |
36 test("a", "a") |
| 485 |
37 test("ab", "ab") |
| 486 |
38 test("abc", "abc") |
| 487 |
39 test("a\n", "a\n") |
| 488 |
40 test("a\nb", "a\nb") |
| 489 |
41 |
| 490 |
42 print "done" |
| 491 |
|
| 492 |
It is also possible to write a 'pure' unit test (one that doesn't have a |
| 493 |
corresponding .out file). The only thing that is needed in addition to the usual |
| 494 |
guidelines for writing Python unit tests is this snippet at the end: |
| 495 |
|
| 496 |
import silenttestrunner |
| 497 |
... |
| 498 |
if __name__ == '__main__': |
| 499 |
silenttestrunner.main(__name__) |
| 500 |
|
| 501 |
## Writing a Python doctest |
| 502 |
|
| 503 |
The Mercurial test suite also supports running Python doctests from the |
| 504 |
docstrings in the source code. This can be useful for testing simple functions |
| 505 |
which don't work on complex data or repositories. Here's an example test from |
| 506 |
mercurial/changelog.py: |
| 507 |
|
| 508 |
def _string_escape(text): |
| 509 |
""" |
| 510 |
>>> d = {'nl': chr(10), 'bs': chr(92), 'cr': chr(13), 'nul': chr(0)} |
| 511 |
>>> s = "ab%(nl)scd%(bs)s%(bs)sn%(nul)sab%(cr)scd%(bs)s%(nl)s" % d |
| 512 |
>>> s |
| 513 |
'ab\\ncd\\\\\\\\n\\x00ab\\rcd\\\\\\n' |
| 514 |
>>> res = _string_escape(s) |
| 515 |
>>> s == res.decode('string_escape') |
| 516 |
True |
| 517 |
""" |
| 518 |
# subset of the string_escape codec |
| 519 |
text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r') |
| 520 |
return text.replace('\0', '\\0') |
| 521 |
|
| 522 |
This tests is run by tests/test-docstring.py, which contains a list of modules |
| 523 |
to search for docstring tests in. |