testament.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Testament</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">Testament</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <div class="search-groupby">
  49. Group by:
  50. <select onchange="groupBy(this.value)">
  51. <option value="section">Section</option>
  52. <option value="type">Type</option>
  53. </select>
  54. </div>
  55. <ul class="simple simple-toc" id="toc-list">
  56. <li><a class="reference" id="test-files-location_toc" href="#test-files-location">Test files location</a></li>
  57. <li><a class="reference" id="commands_toc" href="#commands">Commands</a></li>
  58. <li><a class="reference" id="options_toc" href="#options">Options</a></li>
  59. <li><a class="reference" id="running-a-single-test_toc" href="#running-a-single-test">Running a single test</a></li>
  60. <li><a class="reference" id="running-all-tests-from-a-directory_toc" href="#running-all-tests-from-a-directory">Running all tests from a directory</a></li>
  61. <li><a class="reference" id="html-reports_toc" href="#html-reports">HTML Reports</a></li>
  62. <li><a class="reference" id="writing-unit-tests_toc" href="#writing-unit-tests">Writing Unit tests</a></li>
  63. <ul class="simple"><li><a class="reference" id="writing-unit-tests-inline-hints-warnings-and-errors-notes_toc" href="#writing-unit-tests-inline-hints-warnings-and-errors-notes">Inline hints, warnings and errors (notes)</a></li>
  64. <li><a class="reference" id="writing-unit-tests-output-message-variable-interpolation_toc" href="#writing-unit-tests-output-message-variable-interpolation">Output message variable interpolation</a></li>
  65. <li><a class="reference" id="writing-unit-tests-cmd-variable-interpolation_toc" href="#writing-unit-tests-cmd-variable-interpolation">Cmd variable interpolation</a></li>
  66. </ul><li><a class="reference" id="unit-test-examples_toc" href="#unit-test-examples">Unit test Examples</a></li>
  67. </ul>
  68. </div>
  69. <div class="nine columns" id="content">
  70. <a href="https://github.com/nim-lang/Nim/tree/devel/doc/testament.md#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  71. <a href="https://github.com/nim-lang/Nim/edit/devel/doc/testament.md#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  72. <div id="tocRoot"></div>
  73. <p class="module-desc"><p>Testament is an advanced automatic unittests runner for Nim tests, is used for the development of Nim itself, offers process isolation for your tests, it can generate statistics about test cases, supports multiple targets (C, C++, ObjectiveC, JavaScript, etc.), simulated <a class="reference external" href="https://en.wikipedia.org/wiki/Dry_run_(testing)">Dry-Runs</a>, has logging, can generate HTML reports, skip tests from a file, and more, so can be useful to run your tests, even the most complex ones.</p>
  74. <h1><a class="toc-backref" id="test-files-location" href="#test-files-location">Test files location</a></h1><p>By default, Testament looks for test files on <tt class="docutils literal"><span class="pre">&quot;./tests/category/*.nim&quot;</span></tt>. You can overwrite this pattern glob using <tt class="docutils literal"><span class="pre option">pattern &lt;glob&gt;</span></tt>. The default working directory path can be changed using <tt class="docutils literal"><span class="pre option">--directory:&quot;folder/subfolder/&quot;</span></tt>.</p>
  75. <p>Testament uses the <tt class="docutils literal"><span class="pre"><span class="program">nim</span></span></tt> compiler on <tt class="docutils literal"><span class="pre"><span class="Identifier">PATH</span></span></tt>. You can change that using <tt class="docutils literal"><span class="pre option">--nim:&quot;folder/subfolder/nim&quot;</span></tt>. Running JavaScript tests with <tt class="docutils literal"><span class="pre option">--targets:&quot;js&quot;</span></tt> requires a working NodeJS on <tt class="docutils literal"><span class="pre"><span class="Identifier">PATH</span></span></tt>.</p>
  76. <h1><a class="toc-backref" id="commands" href="#commands">Commands</a></h1><p>p|pat|pattern &lt;glob&gt; run all the tests matching the given pattern all run all tests inside of category folders c|cat|category &lt;category&gt; run all the tests of a certain category r|run &lt;test&gt; run single test file html generate testresults.html from the database</p>
  77. <h1><a class="toc-backref" id="options" href="#options">Options</a></h1><div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--print</span></tt></div><div class="option-list-description">print results to the console</div></div>
  78. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--verbose</span></tt></div><div class="option-list-description">print commands (compiling and running tests)</div></div>
  79. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--simulate</span></tt></div><div class="option-list-description">see what tests would be run but don't run them (for debugging)</div></div>
  80. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--failing</span></tt></div><div class="option-list-description">only show failing/ignored tests</div></div>
  81. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--targets:&quot;c cpp js objc&quot; run tests for specified targets (default: c)</span></tt></div><div class="option-list-description"></div></div>
  82. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--nim:path</span></tt></div><div class="option-list-description">use a particular nim executable (default: $PATH/nim)</div></div>
  83. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--directory:dir</span></tt></div><div class="option-list-description">Change to directory dir before reading the tests or doing anything else.</div></div>
  84. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--colors:on|off</span></tt></div><div class="option-list-description">Turn messages coloring on|off.</div></div>
  85. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--backendLogging:on|off</span></tt></div><div class="option-list-description">Disable or enable backend logging. By default turned on.</div></div>
  86. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--megatest:on|off</span></tt></div><div class="option-list-description">Enable or disable megatest. Default is on.</div></div>
  87. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--valgrind:on|off</span></tt></div><div class="option-list-description">Enable or disable valgrind support. Default is on.</div></div>
  88. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--skipFrom:file</span></tt></div><div class="option-list-description">Read tests to skip from <tt class="docutils literal"><span class="pre"><span class="Identifier">file</span></span></tt> - one test per line, # comments ignored</div></div>
  89. </div>
  90. <h1><a class="toc-backref" id="running-a-single-test" href="#running-a-single-test">Running a single test</a></h1><p>This is a minimal example to understand the basics, not very useful for production, but easy to understand:</p>
  91. <p><pre class="listing"><span class="Prompt">$ </span><span class="program">mkdir</span> <span class="option">-p</span> <span class="Identifier">tests/category</span>
  92. <span class="Prompt">$ </span><span class="program">echo</span> <span class="option">&quot;assert 42 == 42&quot;</span> <span class="option">&gt;</span> <span class="Identifier">tests/category/test0.nim</span>
  93. <span class="Prompt">$ </span><span class="program">testament</span> <span class="option">run</span> <span class="Identifier">tests/category/test0.nim</span>
  94. <span class="ProgramOutput">PASS: tests/category/test0.nim c ( 0.2 sec)</span>
  95. <span class="Prompt">$ </span><span class="program">testament</span> <span class="option">r</span> <span class="Identifier">tests/category/test0</span>
  96. <span class="ProgramOutput">PASS: tests/category/test0.nim C ( 0.2 sec)</span></pre></p>
  97. <h1><a class="toc-backref" id="running-all-tests-from-a-directory" href="#running-all-tests-from-a-directory">Running all tests from a directory</a></h1><p>This will run all tests in the top level tests/ directory. NOTE: these tests are skipped by <tt class="docutils literal"><span class="pre"><span class="Identifier">testament</span> <span class="Identifier">all</span></span></tt>.</p>
  98. <p><pre class="listing"><span class="Prompt">$ </span><span class="program">testament</span> <span class="option">pattern</span> <span class="option">&quot;tests/*.nim&quot;</span></pre></p>
  99. <p>To search for tests deeper in a directory, use</p>
  100. <p><pre class="listing"><span class="Prompt">$ </span><span class="program">testament</span> <span class="option">pattern</span> <span class="option">&quot;tests/**/*.nim&quot;</span> <span class="Comment"># one level deeper</span>
  101. <span class="Prompt">$ </span><span class="program">testament</span> <span class="option">pattern</span> <span class="option">&quot;tests/**/**/*.nim&quot;</span> <span class="Comment"># two levels deeper</span></pre></p>
  102. <h1><a class="toc-backref" id="html-reports" href="#html-reports">HTML Reports</a></h1><p>Generate HTML Reports <tt class="docutils literal"><span class="pre">testresults.html</span></tt> from unittests, you have to run at least 1 test <em>before</em> generating a report:</p>
  103. <p><pre class="listing"><span class="Prompt">$ </span><span class="program">testament</span> <span class="option">html</span></pre></p>
  104. <h1><a class="toc-backref" id="writing-unit-tests" href="#writing-unit-tests">Writing Unit tests</a></h1><p>Example &quot;template&quot; <strong>to edit</strong> and write a Testament unittest:</p>
  105. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  106. # What actions to expect completion on.
  107. # Options:
  108. # &quot;compile&quot;: expect successful compilation
  109. # &quot;run&quot;: expect successful compilation and execution
  110. # &quot;reject&quot;: expect failed compilation. The &quot;reject&quot; action can catch
  111. # {.error.} pragmas but not {.fatal.} pragmas because
  112. # {.error.} calls are expected to originate from the test-file,
  113. # and can explicitly be specified using the &quot;file&quot;, &quot;line&quot; and
  114. # &quot;column&quot; options.
  115. # {.fatal.} pragmas guarantee that compilation will be aborted.
  116. action: &quot;run&quot;
  117. # For testing failed compilations you can specify the expected origin of the
  118. # compilation error.
  119. # With the &quot;file&quot;, &quot;line&quot; and &quot;column&quot; options you can define the file,
  120. # line and column that a compilation-error should have originated from.
  121. # Use only with action: &quot;reject&quot; as it expects a failed compilation.
  122. # Requires errormsg or msg to be defined.
  123. # file: &quot;&quot;
  124. # line: &quot;&quot;
  125. # column: &quot;&quot;
  126. # The exit code that the test is expected to return. Typically, the default
  127. # value of 0 is fine. Note that if the test will be run by valgrind, then
  128. # the test will exit with either a code of 0 on success or 1 on failure.
  129. exitcode: 0
  130. # Provide an `output` string to assert that the test prints to standard out
  131. # exactly the expected string. Provide an `outputsub` string to assert that
  132. # the string given here is a substring of the standard out output of the
  133. # test (the output includes both the compiler and test execution output).
  134. output: &quot;&quot;
  135. outputsub: &quot;&quot;
  136. # Whether to sort the compiler output lines before comparing them to the
  137. # expected output.
  138. sortoutput: true
  139. # Provide a `nimout` string to assert that the compiler during compilation
  140. # prints the defined lines to the standard out.
  141. # The lines must match in order, but there may be more lines that appear
  142. # before, after, or in between them.
  143. nimout: '''
  144. a very long,
  145. multi-line
  146. string'''
  147. # This is the Standard Input the test should take, if any.
  148. input: &quot;&quot;
  149. # Error message the test should print, if any.
  150. errormsg: &quot;&quot;
  151. # Can be run in batch mode, or not.
  152. batchable: true
  153. # Can be run Joined with other tests to run all together, or not.
  154. joinable: true
  155. # On Linux 64-bit machines, whether to use Valgrind to check for bad memory
  156. # accesses or memory leaks. On other architectures, the test will be run
  157. # as-is, without Valgrind.
  158. # Options:
  159. # true: run the test with Valgrind
  160. # false: run the without Valgrind
  161. # &quot;leaks&quot;: run the test with Valgrind, but do not check for memory leaks
  162. valgrind: false # Can use Valgrind to check for memory leaks, or not (Linux 64Bit only).
  163. # Checks that the specified piece of C-code is within the generated C-code.
  164. ccodecheck: &quot;'Assert error message'&quot;
  165. # Command the test should use to run. If left out or an empty string is
  166. # provided, the command is taken to be:
  167. # &quot;nim $target --hints:on -d:testing --nimblePath:build/deps/pkgs $options $file&quot;
  168. # Subject to variable interpolation.
  169. cmd: &quot;nim c -r $file&quot;
  170. # Maximum generated temporary intermediate code file size for the test.
  171. maxcodesize: 666
  172. # Timeout seconds to run the test. Fractional values are supported.
  173. timeout: 1.5
  174. # Targets to run the test into (c, cpp, objc, js). Defaults to c.
  175. targets: &quot;c js&quot;
  176. # flags with which to run the test, delimited by `;`
  177. matrix: &quot;; -d:release; -d:caseFoo -d:release&quot;
  178. # Conditions that will skip this test. Use of multiple &quot;disabled&quot; clauses
  179. # is permitted.
  180. disabled: &quot;bsd&quot; # Can disable OSes...
  181. disabled: &quot;win&quot;
  182. disabled: &quot;32bit&quot; # ...or architectures
  183. disabled: &quot;i386&quot;
  184. disabled: &quot;azure&quot; # ...or pipeline runners
  185. disabled: true # ...or can disable the test entirely
  186. &quot;&quot;&quot;</span>
  187. <span class="Identifier">assert</span> <span class="Identifier">true</span>
  188. <span class="Identifier">assert</span> <span class="DecNumber">42</span> <span class="Operator">==</span> <span class="DecNumber">42</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Assert error message&quot;</span></pre></p>
  189. <ul class="simple"><li>As you can see the &quot;Spec&quot; is just a <tt class="docutils literal"><span class="pre"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot; &quot;&quot;&quot;</span></span></tt>.</li>
  190. <li>Spec has sane defaults, so you don't need to provide them all, any simple assert will work just fine.</li>
  191. <li>This is not the full spec of Testament, check <a class="reference external" href="https://github.com/nim-lang/Nim/blob/devel/testament/specs.nim#L317">the Testament Spec on GitHub, see parseSpec()</a>.</li>
  192. <li>Nim itself uses Testament, so <a class="reference external" href=" https://github.com/nim-lang/Nim/tree/devel/tests">there are plenty of test examples</a>.</li>
  193. <li>Has some built-in CI compatibility, like Azure Pipelines, etc.</li>
  194. </ul>
  195. <h2><a class="toc-backref" id="writing-unit-tests-inline-hints-warnings-and-errors-notes" href="#writing-unit-tests-inline-hints-warnings-and-errors-notes">Inline hints, warnings and errors (notes)</a></h2><p>Testing the line, column, kind and message of hints, warnings and errors can be written inline like so:</p>
  196. <p><pre class="listing"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">warning</span><span class="Punctuation">:</span> <span class="StringLit">&quot;warning!!&quot;</span><span class="Punctuation">}</span> <span class="LongComment">#[tt.Warning
  197. ^ warning!! [User] ]#</span></pre></p>
  198. <p>The opening <tt class="docutils literal"><span class="pre"><span class="LongComment">#[tt.</span></span></tt> marks the message line. The <tt class="docutils literal"><span class="pre"><span class="Operator">^</span></span></tt> marks the message column.</p>
  199. <p>Inline messages can be combined with <tt class="docutils literal"><span class="pre"><span class="Identifier">nimout</span></span></tt> when <tt class="docutils literal"><span class="pre"><span class="Identifier">nimoutFull</span></span></tt> is false (default). This allows testing for expected messages from other modules:</p>
  200. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  201. nimout: &quot;config.nims(1, 1) Hint: some hint message [User]&quot;
  202. &quot;&quot;&quot;</span>
  203. <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">warning</span><span class="Punctuation">:</span> <span class="StringLit">&quot;warning!!&quot;</span><span class="Punctuation">}</span> <span class="LongComment">#[tt.Warning
  204. ^ warning!! [User] ]#</span></pre></p>
  205. <p>Multiple messages for a line can be checked by delimiting messages with ';':</p>
  206. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  207. matrix: &quot;--errorMax:0 --styleCheck:error&quot;
  208. &quot;&quot;&quot;</span>
  209. <span class="Keyword">proc</span> <span class="Identifier">generic_proc</span><span class="Operator">*</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Identifier">a_a</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="LongComment">#[tt.Error
  210. ^ 'generic_proc' should be: 'genericProc'; tt.Error
  211. ^ 'a_a' should be: 'aA' ]#</span>
  212. <span class="Keyword">discard</span></pre></p>
  213. <p>Use <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">errorMax</span><span class="Punctuation">:</span><span class="DecNumber">0</span></span></tt> in <tt class="docutils literal"><span class="pre"><span class="Identifier">matrix</span></span></tt>, or <tt class="docutils literal"><span class="pre"><span class="Identifier">cmd</span><span class="Punctuation">:</span> <span class="StringLit">&quot;nim check $file&quot;</span></span></tt> when testing for multiple 'Error' messages.</p>
  214. <h2><a class="toc-backref" id="writing-unit-tests-output-message-variable-interpolation" href="#writing-unit-tests-output-message-variable-interpolation">Output message variable interpolation</a></h2><p><tt class="docutils literal"><span class="pre"><span class="Identifier">errormsg</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">nimout</span></span></tt>, and inline messages are subject to these variable interpolations:</p>
  215. <ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Punctuation">{</span><span class="Operator">/</span><span class="Punctuation">}</span></span></tt> - platform's directory separator</li>
  216. <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">file</span></span></tt> - the filename (without directory) of the test</li>
  217. </ul>
  218. <p>All other <tt class="docutils literal"><span class="pre"><span class="Operator">$</span></span></tt> characters need escaped as <tt class="docutils literal"><span class="pre"><span class="Operator">$$</span></span></tt>.</p>
  219. <h2><a class="toc-backref" id="writing-unit-tests-cmd-variable-interpolation" href="#writing-unit-tests-cmd-variable-interpolation">Cmd variable interpolation</a></h2><p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">cmd</span></span></tt> option is subject to these variable interpolations:</p>
  220. <ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">target</span></span></tt> - the compilation target, e.g. <tt class="docutils literal"><span class="pre"><span class="Identifier">c</span></span></tt>.</li>
  221. <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">options</span></span></tt> - the options for the compiler.</li>
  222. <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">file</span></span></tt> - the file path of the test.</li>
  223. <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">filedir</span></span></tt> - the directory of the test file.</li>
  224. </ul>
  225. <pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  226. cmd: &quot;nim $target --nimblePath:./nimbleDir/simplePkgs $options $file&quot;
  227. &quot;&quot;&quot;</span></pre><p>All other <tt class="docutils literal"><span class="pre"><span class="Operator">$</span></span></tt> characters need escaped as <tt class="docutils literal"><span class="pre"><span class="Operator">$$</span></span></tt>.</p>
  228. <h1><a class="toc-backref" id="unit-test-examples" href="#unit-test-examples">Unit test Examples</a></h1><p>Expected to fail:</p>
  229. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  230. errormsg: &quot;undeclared identifier: 'not_defined'&quot;
  231. &quot;&quot;&quot;</span>
  232. <span class="Identifier">assert</span> <span class="Identifier">not_defined</span> <span class="Operator">==</span> <span class="StringLit">&quot;not_defined&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;not_defined is not defined&quot;</span></pre></p>
  233. <p>Expected to fail with error thrown from another file:</p>
  234. <p><pre class="listing"><span class="Comment"># test.nim</span>
  235. <span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  236. action: &quot;reject&quot;
  237. errorMsg: &quot;I break&quot;
  238. file: &quot;breakPragma.nim&quot;
  239. &quot;&quot;&quot;</span>
  240. <span class="Keyword">import</span> <span class="Operator">./</span><span class="Identifier">breakPragma</span>
  241. <span class="Keyword">proc</span> <span class="Identifier">x</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">justDo</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Keyword">discard</span>
  242. <span class="Comment"># breakPragma.nim</span>
  243. <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">macros</span>
  244. <span class="Keyword">macro</span> <span class="Identifier">justDo</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">procDef</span><span class="Punctuation">:</span> <span class="Identifier">typed</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">untyped</span> <span class="Operator">=</span>
  245. <span class="Identifier">error</span><span class="Punctuation">(</span><span class="StringLit">&quot;I break&quot;</span><span class="Punctuation">)</span>
  246. <span class="Keyword">return</span> <span class="Identifier">procDef</span></pre></p>
  247. <p>Expecting generated C to contain a given piece of code:</p>
  248. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  249. # Checks that the string &quot;Assert error message&quot; is in the generated
  250. # C code.
  251. ccodecheck: &quot;'Assert error message'&quot;
  252. &quot;&quot;&quot;</span>
  253. <span class="Identifier">assert</span> <span class="DecNumber">42</span> <span class="Operator">==</span> <span class="DecNumber">42</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Assert error message&quot;</span></pre></p>
  254. <p>Non-Zero exit code:</p>
  255. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  256. exitcode: 1
  257. &quot;&quot;&quot;</span>
  258. <span class="Identifier">quit</span> <span class="StringLit">&quot;Non-Zero exit code&quot;</span><span class="Punctuation">,</span> <span class="DecNumber">1</span></pre></p>
  259. <p>Standard output checking:</p>
  260. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  261. output: '''
  262. 0
  263. 1
  264. 2
  265. 3
  266. 4
  267. 5
  268. '''
  269. &quot;&quot;&quot;</span>
  270. <span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">5</span><span class="Punctuation">:</span> <span class="Identifier">echo</span> <span class="Identifier">i</span></pre></p>
  271. <p>JavaScript tests:</p>
  272. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  273. targets: &quot;js&quot;
  274. &quot;&quot;&quot;</span>
  275. <span class="Keyword">when</span> <span class="Identifier">defined</span><span class="Punctuation">(</span><span class="Identifier">js</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  276. <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">jsconsole</span>
  277. <span class="Identifier">console</span><span class="Operator">.</span><span class="Identifier">log</span><span class="Punctuation">(</span><span class="StringLit">&quot;My Frontend Project&quot;</span><span class="Punctuation">)</span></pre></p>
  278. <p>Compile-time tests:</p>
  279. <p><pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;
  280. action: &quot;compile&quot;
  281. &quot;&quot;&quot;</span>
  282. <span class="Keyword">static</span><span class="Punctuation">:</span> <span class="Identifier">assert</span> <span class="DecNumber">9</span> <span class="Operator">==</span> <span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Compile time assert&quot;</span></pre></p>
  283. <p>Tests without Spec:</p>
  284. <p><pre class="listing"><span class="Identifier">assert</span> <span class="DecNumber">1</span> <span class="Operator">==</span> <span class="DecNumber">1</span></pre></p>
  285. <p>See also:</p>
  286. <ul class="simple"><li><a class="reference external" href="unittest.html">Unittest</a></li>
  287. </ul>
  288. </p>
  289. </div>
  290. </div>
  291. <div class="twelve-columns footer">
  292. <span class="nim-sprite"></span>
  293. <br>
  294. <small style="color: var(--hint);">Made with Nim. Generated: 2024-12-25 08:35:35 UTC</small>
  295. </div>
  296. </div>
  297. </div>
  298. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  299. </body>
  300. </html>