eunit.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [[eunit]]
  2. == EUnit
  3. EUnit is the tool of choice for unit testing. Erlang.mk
  4. automates a few things on top of EUnit, including the
  5. discovery and running of unit tests.
  6. === Writing tests
  7. The http://www.erlang.org/doc/apps/eunit/chapter.html[EUnit user guide]
  8. is the best place to learn how to write tests. Of note is
  9. that all functions ending with `_test` or `_test_` will be
  10. picked up as EUnit test cases.
  11. Erlang.mk will automatically pick up tests found in any of
  12. the Erlang modules of your application. It will also pick up
  13. tests located in the '$(TEST_DIR)' directory, which defaults
  14. to 'test/'.
  15. It is generally a good practice to hide test code from
  16. the code you ship to production. With Erlang.mk, you can
  17. do this thanks to the `TEST` macro. It is only defined
  18. when running tests:
  19. [source,erlang]
  20. ----
  21. -ifdef(TEST).
  22. %% Insert tests here.
  23. -endif.
  24. ----
  25. Be careful, however, if you include the EUnit header file,
  26. as it also defines the `TEST` macro. Make sure to only include
  27. it inside an `ifdef` block, otherwise tests will always be
  28. compiled.
  29. [source,erlang]
  30. ----
  31. -ifdef(TEST).
  32. -include_lib(\"eunit/include/eunit.hrl\").
  33. %% Insert tests here.
  34. -endif.
  35. ----
  36. Erlang.mk will automatically recompile your code when you
  37. perform a normal build after running tests, and vice versa.
  38. === Configuration
  39. The `EUNIT_OPTS` variable allows you to specify additional
  40. EUnit options. Options are documented in the
  41. http://www.erlang.org/doc/man/eunit.html#test-2[EUnit manual].
  42. At the time of writing, the only available option is `verbose`:
  43. [source,make]
  44. EUNIT_OPTS = verbose
  45. The `EUNIT_ERL_OPTS` variable allows you to specify options
  46. to be passed to `erl` when running EUnit tests. For example,
  47. you can load the 'vm.args' and 'sys.config' files:
  48. [source,make]
  49. EUNIT_ERL_OPTS = -args_file rel/vm.args -config rel/sys.config
  50. === Usage
  51. To run all tests (including EUnit):
  52. [source,bash]
  53. $ make tests
  54. To run all tests and static checks (including EUnit):
  55. [source,bash]
  56. $ make check
  57. You can also run EUnit separately:
  58. [source,bash]
  59. $ make eunit
  60. EUnit will be quiet by default, only outputting errors.
  61. You can easily make it verbose for a single invocation:
  62. [source,bash]
  63. $ make eunit EUNIT_OPTS=verbose
  64. Erlang.mk allows you to run all tests from a specific
  65. module, or a specific test case from that module, using
  66. the variable `t`.
  67. For example, to run all tests from the `cow_http_hd`
  68. module (instead of all tests from the entire project),
  69. one could write:
  70. [source,bash]
  71. $ make eunit t=cow_http_hd
  72. Similarly, to run a specific test case:
  73. [source,bash]
  74. $ make eunit t=cow_http_hd:parse_accept_test_
  75. To do the same against a multi-application repository,
  76. you can use the `-C` option:
  77. [source,bash]
  78. $ make -C apps/my_app eunit t=my_module:hello_test
  79. Note that this also applies to dependencies. From Cowboy,
  80. you can run the following directly:
  81. [source,bash]
  82. $ make -C deps/cowlib eunit t=cow_http_hd
  83. Finally, xref:coverage[code coverage] is available,
  84. but covered in its own chapter.