4

In RSpec it is possible to create a test and refer to it from multiple places using shared_examples. It simplifies developer's life a lot.

Is there any way to use shared examples in ExUnit?

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142

1 Answers1

1

test macros do some bookkeeping, but ultimately they simply define a function for the test. ExUnit test code is like any other Elixir code, so you can use all the things you normally use to remove duplication. You can use macros to define common tests similar to shared_examples for you or use functions to extract common code that repeats between tests.

michalmuskala
  • 11,028
  • 2
  • 36
  • 47
  • Are there any relevant conventions on how to do this? Say I have some parameterized tests and a set of sets of parameters that I want to use for all of those tests. I can easily define a list or a collection of that set of sets of parameters, but what's a suitable place to put that file in my project? – Kenny Evitt Jul 12 '19 at 19:01
  • After thinking about it for a little bit, putting, e.g. a shared function, in a module in a file under the *test/support* directory (or a sub-directory of it) seems pretty reasonable. – Kenny Evitt Jul 12 '19 at 21:41