Proposal: ExUnit nested tests within describe block can inherit describe's test module attributes

314 views
Skip to first unread message

Brian Cardarella

unread,
Mar 31, 2017, 4:45:59 PM3/31/17
to elixir-lang-core
Currently if you do:

    ExUnit.Case.register_attribute(__MODULE__, :foo)

    @foo :bar
    describe "outer" do
      test "inner 1", %{registered: %{foo: foo}} do
        IO.inspect(foo)
      end

      test "inner 2", %{registered: %{foo: foo}} do
        IO.inspect(foo)
      end
    end

The result will print `:bar` when "outer inner 1" runs and `nil` when "outer inner 2" runs because the test module attribute values reset after each `test/2`

It would be great if the test module attributes declared above a `describe` could be inherited by all tested `test/2`s.

If there isn't opposition to this idea I can work on implementing it.
        

José Valim

unread,
Mar 31, 2017, 7:24:12 PM3/31/17
to elixir-l...@googlegroups.com
There is @describetag that can be used inside the describe block.

    describe "outer" do
     @describetag foo :bar
      test "inner 1", %{registered: %{foo: foo}} do
        IO.inspect(foo)
      end

      test "inner 2", %{registered: %{foo: foo}} do
        IO.inspect(foo)
      end
    end

Wouldn't that be enough?



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/db59da0f-64bd-4079-aeb0-0249ddc54066%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Cardarella

unread,
Mar 31, 2017, 8:15:02 PM3/31/17
to elixir-lang-core, jose....@plataformatec.com.br
Here is my usecase, in ecto_fixtures you can declar via macros the fixture state to bring into each test:

    fixture :employee_1
    context :admin
    serialize true
    test "foo bar", %{data: data} do
      ...
    end

Quite often the entire test module would be asserting within an admin context. It would be great to do something like:

    context :admin
    describe "Admin" do
      fixture :employee_
      serialize true
      test "foo bar", %{data: data} do
        ...
      end

      fixture :employee_2
      test "foo bar", %{data: data} do
        ...
      end
    end

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages