testament.rst 7.6 KB

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