Testing tips

If you have any testing tips or best practices to share please do so in this thread :slight_smile:

4 Likes

I found the following helpful in my development.

  • Unit testing
  • Wrapping tests in describe blocks to group like tests together (in lieu of tags)

  • Setting up mocks and behaviours for APIs within your application or those that talk to external parties. I like setting up the configuration for these in the following way:

    config/dev.exs

    config :my_app, Adapters,
    my_module: MyApp.MyModule.Live

    config/test.ex

    config :my_app, Adapters,
    my_module: MyApp.MyModule.Mock

    module that uses MyModule

    @my_app Application.get_env(:my_app, Adapters)

    use

    def fun(args), do: @my_app[:my_module].my_function(args)

  • ExUnit
  • Using CaseTemplate to create custom cases which import helper modules and setup your cases (great example by Kat Tornwall from ElixirConf 2016)
  • Test Data

  • I use ExMachina for test data (the Ecto adapter is VERY helpul)

  • External APIs

  • For actually testing external HTTP clients, I like using ExVCR to record the response I want and make sure it is matched when I run my tests

I’d love to get others’ input on their testing techniques, integration tests in particular. I’d also like to hear how people use bypass without Phoenix.

8 Likes

I want to know something?.. Is there any Rspec like alternative In Elixir?

Actually moreover I want to know is there any library you people prefer…

1 Like

There’s Espec, but I think most people just use ExUnit :slight_smile:

2 Likes

Thanks

2 Likes

A language and DSL agnostic tip I would share is your tests are your code too.

Every so often it pays to look at your test suite and notice if there are things you can improve (normally following DRY).

Occasionally you may even find dead tests (tests that are providing no value, either because you’re testing something twice or the code you’re executing doesn’t actually serve a business purpose any more).

If you’re following some sort of TDD practice you will likely be spending a lot of your day in test code, the nicer that test code is the better you’ll feel about spending time in it!

2 Likes

@wfgilman - thank you for posting your thoughts. A lot of stuff in there, very succintly put. I’m trying to get MUCH better at testing and this post is a great start.