testament.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Testament is an advanced automatic unittests runner for Nim tests, is used for the development of Nim itself,
  2. offers process isolation for your tests, it can generate statistics about test cases,
  3. supports multiple targets (C, C++, ObjectiveC, JavaScript, etc),
  4. simulated `Dry-Runs <https://en.wikipedia.org/wiki/Dry_run_(testing)>`_,
  5. has logging, can generate HTML reports, skip tests from a file, and more,
  6. so can be useful to run your tests, even the most complex ones.
  7. Test files location
  8. ===================
  9. By default Testament looks for test files on ``"./tests/*.nim"``.
  10. You can overwrite this pattern glob using ``pattern <glob>``.
  11. The default working directory path can be changed using
  12. ``--directory:"folder/subfolder/"``.
  13. Testament uses the ``nim`` compiler on ``PATH``.
  14. You can change that using ``--nim:"folder/subfolder/nim"``.
  15. Running JavaScript tests with ``--targets:"js"`` requires a working NodeJS on
  16. ``PATH``.
  17. Options
  18. =======
  19. * ``--print`` Also print results to the console
  20. * ``--simulate`` See what tests would be run but don't run them (for debugging)
  21. * ``--failing`` Only show failing/ignored tests
  22. * ``--targets:"c cpp js objc"`` Run tests for specified targets (default: all)
  23. * ``--nim:path`` Use a particular nim executable (default: ``$PATH/nim``)
  24. * ``--directory:dir`` Change to directory dir before reading the tests or doing anything else.
  25. * ``--colors:on|off`` Turn messages coloring on|off.
  26. * ``--backendLogging:on|off`` Disable or enable backend logging. By default turned on.
  27. * ``--skipFrom:file`` Read tests to skip from ``file`` - one test per line, # comments ignored
  28. Running a single test
  29. =====================
  30. This is a minimal example to understand the basics,
  31. not very useful for production, but easy to understand:
  32. .. code::
  33. $ mkdir tests
  34. $ echo "assert 42 == 42" > tests/test0.nim
  35. $ testament run test0.nim
  36. PASS: tests/test0.nim C ( 0.2 sec)
  37. $ testament r test0
  38. PASS: tests/test0.nim C ( 0.2 sec)
  39. Running all tests from a directory
  40. ==================================
  41. .. code::
  42. $ testament pattern "tests/*.nim"
  43. To search for tests deeper in a directory, use
  44. .. code::
  45. $ testament pattern "tests/**/*.nim" # one level deeper
  46. $ testament pattern "tests/**/**/*.nim" # two levels deeper
  47. HTML Reports
  48. ============
  49. Generate HTML Reports ``testresults.html`` from unittests,
  50. you have to run at least 1 test *before* generating a report:
  51. .. code::
  52. $ testament html
  53. Writing Unitests
  54. ================
  55. Example "template" **to edit** and write a Testament unittest:
  56. .. code-block:: nim
  57. discard """
  58. # What actions to expect completion on.
  59. # Options:
  60. # "compile": expect successful compilation
  61. # "run": expect successful compilation and execution
  62. # "reject": expect failed compilation. The "reject" action can catch
  63. # {.error.} pragmas but not {.fatal.} pragmas because
  64. # {.fatal.} pragmas guarantee that compilation will be aborted.
  65. action: "run"
  66. # The exit code that the test is expected to return. Typically, the default
  67. # value of 0 is fine. Note that if the test will be run by valgrind, then
  68. # the test will exit with either a code of 0 on success or 1 on failure.
  69. exitcode: 0
  70. # Provide an `output` string to assert that the test prints to standard out
  71. # exatly the expected string. Provide an `outputsub` string to assert that
  72. # the string given here is a substring of the standard out output of the
  73. # test.
  74. output: ""
  75. outputsub: ""
  76. # Whether to sort the output lines before comparing them to the desired
  77. # output.
  78. sortoutput: true
  79. # Each line in the string given here appears in the same order in the
  80. # compiler output, but there may be more lines that appear before, after, or
  81. # in between them.
  82. nimout: '''
  83. a very long,
  84. multi-line
  85. string'''
  86. # This is the Standard Input the test should take, if any.
  87. input: ""
  88. # Error message the test should print, if any.
  89. errormsg: ""
  90. # Can be run in batch mode, or not.
  91. batchable: true
  92. # Can be run Joined with other tests to run all togheter, or not.
  93. joinable: true
  94. # On Linux 64-bit machines, whether to use Valgrind to check for bad memory
  95. # accesses or memory leaks. On other architectures, the test will be run
  96. # as-is, without Valgrind.
  97. # Options:
  98. # true: run the test with Valgrind
  99. # false: run the without Valgrind
  100. # "leaks": run the test with Valgrind, but do not check for memory leaks
  101. valgrind: false # Can use Valgrind to check for memory leaks, or not (Linux 64Bit only).
  102. # Command the test should use to run. If left out or an empty string is
  103. # provided, the command is taken to be:
  104. # "nim $target --hints:on -d:testing --nimblePath:tests/deps $options $file"
  105. # You can use the $target, $options, and $file placeholders in your own
  106. # command, too.
  107. cmd: "nim c -r $file"
  108. # Maximum generated temporary intermediate code file size for the test.
  109. maxcodesize: 666
  110. # Timeout seconds to run the test. Fractional values are supported.
  111. timeout: 1.5
  112. # Targets to run the test into (C, C++, JavaScript, etc).
  113. target: "c js"
  114. # Conditions that will skip this test. Use of multiple "disabled" clauses
  115. # is permitted.
  116. disabled: "bsd" # Can disable OSes...
  117. disabled: "win"
  118. disabled: "32bit" # ...or architectures
  119. disabled: "i386"
  120. disabled: "azure" # ...or pipeline runners
  121. disabled: true # ...or can disable the test entirely
  122. """
  123. assert true
  124. assert 42 == 42, "Assert error message"
  125. * As you can see the "Spec" is just a ``discard """ """``.
  126. * Spec has sane defaults, so you don't need to provide them all, any simple assert will work just fine.
  127. * `This is not the full spec of Testament, check the Testament Spec on GitHub, see parseSpec(). <https://github.com/nim-lang/Nim/blob/devel/testament/specs.nim#L238>`_
  128. * `Nim itself uses Testament, so there are plenty of test examples. <https://github.com/nim-lang/Nim/tree/devel/tests>`_
  129. * Has some built-in CI compatibility, like Azure Pipelines, etc.
  130. * `Testament supports inlined error messages on Unittests, basically comments with the expected error directly on the code. <https://github.com/nim-lang/Nim/blob/9a110047cbe2826b1d4afe63e3a1f5a08422b73f/tests/effects/teffects1.nim>`_
  131. Unitests Examples
  132. =================
  133. Expected to fail:
  134. .. code-block:: nim
  135. discard """
  136. errormsg: "undeclared identifier: 'not_defined'"
  137. """
  138. assert not_defined == "not_defined", "not_defined is not defined"
  139. Non-Zero exit code:
  140. .. code-block:: nim
  141. discard """
  142. exitcode: 1
  143. """
  144. quit "Non-Zero exit code", 1
  145. Standard output checking:
  146. .. code-block:: nim
  147. discard """
  148. output: '''
  149. 0
  150. 1
  151. 2
  152. 3
  153. 4
  154. 5
  155. '''
  156. """
  157. for i in 0..5: echo i
  158. JavaScript tests:
  159. .. code-block:: nim
  160. discard """
  161. target: "js"
  162. """
  163. when defined(js):
  164. import jsconsole
  165. console.log("My Frontend Project")
  166. Compile-time tests:
  167. .. code-block:: nim
  168. discard """
  169. action: "compile"
  170. """
  171. static: assert 9 == 9, "Compile time assert"
  172. Tests without Spec:
  173. .. code-block:: nim
  174. assert 1 == 1
  175. See also:
  176. * `Unittest <unittest.html>`_