testing.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. *testing.txt* Nvim
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Testing Vim and Vim script *testing-support*
  4. Expression evaluation is explained in |eval.txt|. This file goes into details
  5. about writing tests in Vim script. This can be used for testing Vim itself
  6. and for testing plugins.
  7. 1. Testing Vim |testing|
  8. 2. Test functions |test-functions-details|
  9. 3. Assert functions |assert-functions-details|
  10. ==============================================================================
  11. 1. Testing Vim *testing*
  12. Vim can be tested after building it, usually with "make test".
  13. The tests are located in the directory "src/testdir".
  14. There are several types of tests added over time:
  15. test33.in oldest, don't add any of these
  16. test_something.in old style tests
  17. test_something.vim new style tests
  18. *new-style-testing*
  19. New tests should be added as new style tests. These use functions such as
  20. |assert_equal()| to keep the test commands and the expected result in one
  21. place.
  22. *old-style-testing*
  23. In some cases an old style test needs to be used.
  24. Find more information in the file src/testdir/README.txt.
  25. ==============================================================================
  26. 2. Test functions *test-functions-details*
  27. test_garbagecollect_now() *test_garbagecollect_now()*
  28. Like garbagecollect(), but executed right away. This must
  29. only be called directly to avoid any structure to exist
  30. internally, and |v:testing| must have been set before calling
  31. any function.
  32. ==============================================================================
  33. 3. Assert functions *assert-functions-details*
  34. assert_beeps({cmd}) *assert_beeps()*
  35. Run {cmd} and add an error message to |v:errors| if it does
  36. NOT produce a beep or visual bell.
  37. Also see |assert_fails()|, |assert_nobeep()| and
  38. |assert-return|.
  39. Can also be used as a |method|: >
  40. GetCmd()->assert_beeps()
  41. <
  42. *assert_equal()*
  43. assert_equal({expected}, {actual} [, {msg}])
  44. When {expected} and {actual} are not equal an error message is
  45. added to |v:errors| and 1 is returned. Otherwise zero is
  46. returned |assert-return|.
  47. There is no automatic conversion, the String "4" is different
  48. from the Number 4. And the number 4 is different from the
  49. Float 4.0. The value of 'ignorecase' is not used here, case
  50. always matters.
  51. When {msg} is omitted an error in the form "Expected
  52. {expected} but got {actual}" is produced.
  53. Example: >
  54. assert_equal('foo', 'bar')
  55. < Will result in a string to be added to |v:errors|:
  56. test.vim line 12: Expected 'foo' but got 'bar' ~
  57. Can also be used as a |method|: >
  58. mylist->assert_equal([1, 2, 3])
  59. < *assert_equalfile()*
  60. assert_equalfile({fname-one}, {fname-two})
  61. When the files {fname-one} and {fname-two} do not contain
  62. exactly the same text an error message is added to |v:errors|.
  63. Also see |assert-return|.
  64. When {fname-one} or {fname-two} does not exist the error will
  65. mention that.
  66. Can also be used as a |method|: >
  67. GetLog()->assert_equalfile('expected.log')
  68. assert_exception({error} [, {msg}]) *assert_exception()*
  69. When v:exception does not contain the string {error} an error
  70. message is added to |v:errors|. Also see |assert-return|.
  71. This can be used to assert that a command throws an exception.
  72. Using the error number, followed by a colon, avoids problems
  73. with translations: >
  74. try
  75. commandthatfails
  76. call assert_false(1, 'command should have failed')
  77. catch
  78. call assert_exception('E492:')
  79. endtry
  80. assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
  81. Run {cmd} and add an error message to |v:errors| if it does
  82. NOT produce an error. Also see |assert-return|.
  83. When {error} is given it must match in |v:errmsg|.
  84. Note that beeping is not considered an error, and some failing
  85. commands only beep. Use |assert_beeps()| for those.
  86. Can also be used as a |method|: >
  87. GetCmd()->assert_fails('E99:')
  88. assert_false({actual} [, {msg}]) *assert_false()*
  89. When {actual} is not false an error message is added to
  90. |v:errors|, like with |assert_equal()|.
  91. Also see |assert-return|.
  92. A value is false when it is zero. When {actual} is not a
  93. number the assert fails.
  94. When {msg} is omitted an error in the form
  95. "Expected False but got {actual}" is produced.
  96. Can also be used as a |method|: >
  97. GetResult()->assert_false()
  98. assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
  99. This asserts number and |Float| values. When {actual} is lower
  100. than {lower} or higher than {upper} an error message is added
  101. to |v:errors|. Also see |assert-return|.
  102. When {msg} is omitted an error in the form
  103. "Expected range {lower} - {upper}, but got {actual}" is
  104. produced.
  105. *assert_match()*
  106. assert_match({pattern}, {actual} [, {msg}])
  107. When {pattern} does not match {actual} an error message is
  108. added to |v:errors|. Also see |assert-return|.
  109. {pattern} is used as with |expr-=~|: The matching is always done
  110. like 'magic' was set and 'cpoptions' is empty, no matter what
  111. the actual value of 'magic' or 'cpoptions' is.
  112. {actual} is used as a string, automatic conversion applies.
  113. Use "^" and "$" to match with the start and end of the text.
  114. Use both to match the whole text.
  115. When {msg} is omitted an error in the form
  116. "Pattern {pattern} does not match {actual}" is produced.
  117. Example: >
  118. assert_match('^f.*o$', 'foobar')
  119. < Will result in a string to be added to |v:errors|:
  120. test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
  121. Can also be used as a |method|: >
  122. getFile()->assert_match('foo.*')
  123. <
  124. assert_nobeep({cmd}) *assert_nobeep()*
  125. Run {cmd} and add an error message to |v:errors| if it
  126. produces a beep or visual bell.
  127. Also see |assert_beeps()|.
  128. Can also be used as a |method|: >
  129. GetCmd()->assert_nobeep()
  130. <
  131. *assert_notequal()*
  132. assert_notequal({expected}, {actual} [, {msg}])
  133. The opposite of `assert_equal()`: add an error message to
  134. |v:errors| when {expected} and {actual} are equal.
  135. Also see |assert-return|.
  136. Can also be used as a |method|: >
  137. mylist->assert_notequal([1, 2, 3])
  138. < *assert_notmatch()*
  139. assert_notmatch({pattern}, {actual} [, {msg}])
  140. The opposite of `assert_match()`: add an error message to
  141. |v:errors| when {pattern} matches {actual}.
  142. Also see |assert-return|.
  143. Can also be used as a |method|: >
  144. getFile()->assert_notmatch('bar.*')
  145. assert_report({msg}) *assert_report()*
  146. Report a test failure directly, using String {msg}.
  147. Always returns one.
  148. Can also be used as a |method|: >
  149. GetMessage()->assert_report()
  150. assert_true({actual} [, {msg}]) *assert_true()*
  151. When {actual} is not true an error message is added to
  152. |v:errors|, like with |assert_equal()|.
  153. Also see |assert-return|.
  154. A value is |TRUE| when it is a non-zero number or |v:true|.
  155. When {actual} is not a number or |v:true| the assert fails.
  156. When {msg} is omitted an error in the form "Expected True but
  157. got {actual}" is produced.
  158. Can also be used as a |method|: >
  159. GetResult()->assert_true()
  160. <
  161. vim:tw=78:ts=8:noet:ft=help:norl: