Proposal: always expose ExUnit.configuration[:seed]

101 views
Skip to first unread message

Myron Marston

unread,
May 5, 2016, 11:49:48 PM5/5/16
to elixir-lang-core

Currently ExUnit.configuration only exposes config values that the user has explicitly set. That means that if you pass --seed, ExUnit.configuration[:seed] will be available, but if you do not pass --seed, and allow ExUnit to set the seed, it will not be available.

I’ve got a test that winds up involving some randomization. in the off chance that a particular random value cause the test to fail, I’d like to seed randomization in the test setup with the same seed that ExUnit is already using, but it’s difficult since ExUnit.configuration[:seed] is not always available. I came up with a work around in test_helper.exs:

existing_or_new_seed =
  case ExUnit.configuration[:seed] do
    nil -> :os.timestamp |> elem(2)
    existing_seed -> existing_seed
  end

ExUnit.configure(seed: existing_or_new_seed)

…but it feels like I shouldn’t have to jump through those hoops. (And it’s unfortunate that I’ve had to copy the code to provide a seed from ExUnit).

Would the Elixir team be amenable to a PR that ensures that ExUnit.configuration[:seed] is always available, regardless of whether ExUnit set the value or the user set the value via --seed?

Thanks!
Myron

José Valim

unread,
May 6, 2016, 5:35:53 AM5/6/16
to elixir-l...@googlegroups.com
There is no way we can expose the seed because it needs to be generated *per run*. It cannot be a unique value used for when you start it. Fortunately, there are simpler ways of implementing the code above:

seed = ExUnit.configuration[:seed] || :rand.seed(10000)
ExUnit.configure(seed: seed)



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/CADUxQmu6M%3D%3D9_98PpFDGBX5GdCj%2BORRBmz_6TemEXvxzgCjJTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Myron Marston

unread,
May 6, 2016, 1:23:33 PM5/6/16
to elixir-lang-core
> There is no way we can expose the seed because it needs to be generated *per run*.

Why does the fact that it needs to be generated *per run* mean it cannot be exposed?  It's exposed internally to whatever part of ExUnit orders the tests, so why can't it be exposed publicly?

José Valim

unread,
May 6, 2016, 1:40:55 PM5/6/16
to elixir-l...@googlegroups.com
Why does the fact that it needs to be generated *per run* mean it cannot be exposed?  It's exposed internally to whatever part of ExUnit orders the tests, so why can't it be exposed publicly?

Because it is generated only when the suite starts and, in your code, the suite has not started running yet.

Myron Marston

unread,
May 6, 2016, 1:45:14 PM5/6/16
to elixir-lang-core
The point I need it is in a certain test, long after the suite has started.  The work around I talked about in `test_helper.exs` was just a way to ensure it was set.

Also, is there a reason `seed` couldn't be generated and assigned after processing CLI args (at which point ExUnit should be able to know it needs to set the seed)?

On Fri, May 6, 2016 at 10:40 AM, José Valim <jose....@plataformatec.com.br> wrote:
Why does the fact that it needs to be generated *per run* mean it cannot be exposed?  It's exposed internally to whatever part of ExUnit orders the tests, so why can't it be exposed publicly?

Because it is generated only when the suite starts and, in your code, the suite has not started running yet.

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/gDHHiAfYzDc/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAGnRm4%2Btm3q8rB4LWJmySyF6BAOUS8d6KM84ziNyo2KB1rT51A%40mail.gmail.com.

José Valim

unread,
May 6, 2016, 2:04:20 PM5/6/16
to elixir-l...@googlegroups.com
The point I need it is in a certain test, long after the suite has started.  The work around I talked about in `test_helper.exs` was just a way to ensure it was set.

Also, is there a reason `seed` couldn't be generated and assigned after processing CLI args (at which point ExUnit should be able to know it needs to set the seed)?

Not necessarily because you can configure ExUnit and run it multiple times, in any order. That's how umbrella applications run tests for example.

Although there is nothing stopping Mix setting the seed instead of waiting for the runner to set it. But then it wouldn't be different than what you are doing right now, it would just be done by Mix.
Reply all
Reply to author
Forward
0 new messages