Proposal: ex_unit test data provider

108 views
Skip to first unread message

pgo...@xerpa.com.br

unread,
Mar 18, 2019, 1:16:28 PM3/18/19
to elixir-lang-core
Hi everybody,

I thought to implement data providers in elixir unit tests.
It would be like this:

defmodule MyTest do
  use ExUnit.Case

  @contexts [
    "my_value_1",
    %{my_value: 2},
    3
  ]

  describe "my test case" do
    @data_provider @contexts
    test "my object tests", %{context: %{my_value: value}} do
      # It will match once
      assert value == 2
    end

    @data_provider @contexts
    test "", %{context: item} do
      # It will run this function tree times,
      # for each item in @contexts. item value on each loop execution:
      #
      # - "my_value"
      # - %{my_value: 2}
      # - 3
    end
  end
end


It already exists in other languages and I think that it is very useful.

Best regards!

fwat...@xerpa.com.br

unread,
Mar 18, 2019, 2:08:56 PM3/18/19
to elixir-lang-core
describe "my test case" do

   
test_cases_for ["should do something with users", "should do something else with users"], do:
      
[
       
    %{username: "Foobar1", likes_to: ["play soccer"]},
           %{username: "Foobar2", likes_to: ["play basketball", "go on a trip"]},
           %{username: "Foobar3", likes_to: []},
      
]

    #Loops into test_cases
    test
"should do something with users", cases: %{username: username, likes_to: likes_to} do
       #Do something with those params

   
end


    test "should do something else with users", cases: %{username: username, likes_to: likes_to} do
       #Do something with those params

   
end
end

I think it would be cool if ExUnit had something like this, isn`t it?

Andrea Leopardi

unread,
Mar 18, 2019, 3:15:37 PM3/18/19
to elixir-l...@googlegroups.com
We usually try to keep the core language as simple as possible. However Elixir provides all the extensibility that allows you to implement something like this yourself, for example by defining your own test macros. We usually advise to start with a library and see how it goes, and I think that would be a great idea here as well!

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/36931be4-2d15-435b-8d64-f9829f6a0dac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Andrea Leopardi
Reply all
Reply to author
Forward
0 new messages