testament.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ``--directory:"folder/subfolder/"``.
  12. Testament uses the nim compiler on ``PATH`` you can change that using ``--nim:"folder/subfolder/nim"``,
  13. running JavaScript tests with ``--targets:"js"`` requires a working NodeJS on ``PATH``.
  14. Options
  15. =======
  16. * ``--print`` Also print results to the console
  17. * ``--simulate`` See what tests would be run but don't run them (for debugging)
  18. * ``--failing`` Only show failing/ignored tests
  19. * ``--targets:"c c++ js objc"`` Run tests for specified targets (default: all)
  20. * ``--nim:path`` Use a particular nim executable (default: ``$PATH/nim``)
  21. * ``--directory:dir`` Change to directory dir before reading the tests or doing anything else.
  22. * ``--colors:on|off`` Turn messages coloring on|off.
  23. * ``--backendLogging:on|off`` Disable or enable backend logging. By default turned on.
  24. * ``--skipFrom:file`` Read tests to skip from ``file`` - one test per line, # comments ignored
  25. Running a single test
  26. =====================
  27. This is a minimal example to understand the basics,
  28. not very useful for production, but easy to understand:
  29. .. code::
  30. $ mkdir tests
  31. $ echo "assert 42 == 42" > tests/test0.nim
  32. $ testament run test0.nim
  33. PASS: tests/test0.nim C ( 0.2 sec)
  34. $ testament r test0
  35. PASS: tests/test0.nim C ( 0.2 sec)
  36. $
  37. Running all tests from a directory
  38. ==================================
  39. .. code::
  40. $ testament pattern "tests/*.nim"
  41. HTML Reports
  42. ============
  43. Generate HTML Reports ``testresults.html`` from unittests,
  44. you have to run at least 1 test *before* generating a report:
  45. .. code::
  46. $ testament html
  47. Writing Unitests
  48. ================
  49. Example "template" **to edit** and write a Testament unittest:
  50. .. code-block:: nim
  51. discard """
  52. action: "run" # What to do, one of "compile" OR "run".
  53. exitcode: 0 # This is the Exit Code the test should return, zero typically.
  54. output: "" # This is the Standard Output the test should print, if any.
  55. input: "" # This is the Standard Input the test should take, if any.
  56. errormsg: "" # Error message the test should print, if any.
  57. batchable: true # Can be run in batch mode, or not.
  58. joinable: true # Can be run Joined with other tests to run all togheter, or not.
  59. valgrind: false # Can use Valgrind to check for memory leaks, or not (Linux 64Bit only).
  60. cmd: "nim c -r $file" # Command the test should use to run.
  61. maxcodesize: 666 # Maximum generated temporary intermediate code file size for the test.
  62. timeout: 666 # Timeout microseconds to run the test.
  63. target: "c js" # Targets to run the test into (C, C++, JavaScript, etc).
  64. disabled: "bsd" # Disable the test by condition, here BSD is disabled just as an example.
  65. disabled: "win" # Can disable multiple OSes at once
  66. disabled: "32bit" # ...or architectures
  67. disabled: "i386"
  68. disabled: "azure" # ...or pipeline runners
  69. disabled: true # ...or can disable the test entirely
  70. """
  71. assert true
  72. assert 42 == 42, "Assert error message"
  73. * As you can see the "Spec" is just a ``discard """ """``.
  74. * Spec has sane defaults, so you dont need to provide them all, any simple assert will work Ok.
  75. * `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>`_
  76. * `Nim itself uses Testament, so theres plenty of test examples. <https://github.com/nim-lang/Nim/tree/devel/tests>`_
  77. * Has some built-in CI compatibility, like Azure Pipelines, etc.
  78. * `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>`_
  79. Unitests Examples
  80. =================
  81. Expected to fail:
  82. .. code-block:: nim
  83. discard """
  84. errormsg: "undeclared identifier: 'not_defined'"
  85. """
  86. assert not_defined == "not_defined", "not_defined is not defined"
  87. Non-Zero exit code:
  88. .. code-block:: nim
  89. discard """
  90. exitcode: 1
  91. """
  92. quit "Non-Zero exit code", 1
  93. Standard output checking:
  94. .. code-block:: nim
  95. discard """
  96. output: '''
  97. 0
  98. 1
  99. 2
  100. 3
  101. 4
  102. 5
  103. '''
  104. """
  105. for i in 0..5: echo i
  106. JavaScript tests:
  107. .. code-block:: nim
  108. discard """
  109. target: "js"
  110. """
  111. when defined(js):
  112. import jsconsole
  113. console.log("My Frontend Project")
  114. Compile time tests:
  115. .. code-block:: nim
  116. discard """
  117. action: "compile"
  118. """
  119. static: assert 9 == 9, "Compile time assert"
  120. Tests without Spec:
  121. .. code-block:: nim
  122. assert 1 == 1
  123. See also:
  124. * `Unittest <unittest.html>`_