Using @tag type: :property in ExUnit tests

69 views
Skip to first unread message

Thomas Arts

unread,
May 17, 2016, 2:47:58 AM5/17/16
to elixir-lang-core
Thanks to Eric and Valim we now have the possiblity to provide a QuickCheck test outcome as:

Finished in 1.4 seconds
5 properties, 1 failure

Instead of
Finished in 1.4 seconds
5 tests, 1 failure

when using properties instead of tests.
The solution is created by the more general type tests can have.

Now, I wonder whether we can also use this on line 260 in ExUnit.Case

  quote bind_quoted: binding do
      test = :"test #{message}"
      ExUnit.Case.__on_definition__(__ENV__, test, [])
      def unquote(test)(unquote(var)), do: unquote(contents)
    end

Here we see a hard-coded "test" in the code (and on some more places as well).
It would be cool to replace it with the type. I checked it quickly and it seems to work for arbitrary text,
but I don't see how I get the tags into this function.

The result would be for failing tests:

  1) property Property Instructions schema (InstructionsTest)
     test/my_web_Instructions_eqc.exs:5
     forall(json <- MyWeb.Type.Instructions.generate()) do
       ensure(MyWeb.Type.Instructions.validate(json) == :ok)
     end
     Failed for


Another fancy thing that I would have preferred over 5 properties is
5 properties (511 tests), 1 failure

That is, to get access to the test counter and use the num_tests tag to increase the number.

José Valim

unread,
May 17, 2016, 3:15:52 AM5/17/16
to elixir-l...@googlegroups.com
Thomas, I have made a function called register_test/3 public, this means you should be able to mimic the test/3 implementation in your code:

    defmacro property(message, var \\ quote(do: _), contents) do
      contents =
        case contents do
          [do: block] ->
            quote do
              unquote(block)
              :ok
            end
          _ ->
            quote do
              try(unquote(contents))
              :ok
            end
        end

      var      = Macro.escape(var)
      contents = Macro.escape(contents, unquote: true)

      quote bind_quoted: binding do
        property = :"property #{message}"
        ExUnit.Case.register_test(__ENV__, test, [[type: :property]])
        def unquote(property)(unquote(var)), do: unquote(contents)
      end
    end


All of the functions in the code sample above are public API, so you can rely on them. :) I will look into implementing a sub-counter for the properties. That will probably be possible if we implement a counter for assertions. So we would see:

    11 tests (25 assertions)

And in your case:

    11 properties (431 tests)




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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/a07c20da-60d7-4100-bf8b-ba673b0c2e1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Arts

unread,
May 17, 2016, 7:54:48 AM5/17/16
to elixir-lang-core
That seems a good solution Jose. I'll try to get that in as well to be prepared for the real cool thing: counting the tests!

/Thomas

Reply all
Reply to author
Forward
0 new messages