Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run scenarios in fully random order #970

Merged
merged 2 commits into from
Apr 19, 2016

Conversation

threedaymonk
Copy link
Contributor

This documents the desired behaviour of the random order option across multiple features and corrects the ordering of filters within runtime.rb in order to run scenarios in a genuinely random order.

Previously, using --order=random resulted in the order of scenarios being randomised within a feature, but features themselves always proceeded in alphabetical (or ASCIIbetical) order based on their file path, i.e.:

Feature: 1
  Scenario: 1B
  Scenario: 1A

Feature: 2
  Scenario: 2B
  Scenario: 2A

Although Randomizer does indeed shuffle the scenarios, it was followed in the filter chain by LocationsFilter which groups scenarios by file path. Moving Randomizer later in the chain avoids this.

Now, each scenario is run in random order regardless of the name or path of the feature:

Feature: 1
  Scenario: 1A

Feature: 2
  Scenario: 2B
  Scenario: 2A

Feature: 1
  Scenario: 1B

Fixes #969.

This documents the desired behaviour of the random order option across
multiple features and corrects the ordering of filters within runtime.rb
in order to run scenarios in a genuinely random order.

Previously, using --order=random resulted in the order of scenarios
being randomised within a feature, but features themselves always
proceeded in alphabetical (or ASCIIbetical) order based on their file
path, i.e.:

    Feature: 1
      Scenario: 1B
      Scenario: 1A

    Feature: 2
      Scenario: 2B
      Scenario: 2A

Although Randomizer does indeed shuffle the scenarios, it was followed
in the filter chain by LocationsFilter which groups scenarios by file
path. Moving Randomizer later in the chain avoids this.

Now, each scenario is run in random order regardless of the name or path
of the feature:

    Feature: 1
      Scenario: 1A

    Feature: 2
      Scenario: 2B
      Scenario: 2A

    Feature: 1
      Scenario: 1B

See also cucumber#969
@threedaymonk
Copy link
Contributor Author

Well, this is difficult: the feature depends on the seeded random number generator behaving predictably, but the results on Ruby 1.9 and JRuby are different from Ruby 2.x.

@threedaymonk
Copy link
Contributor Author

The problem isn't Random itself, but the implementation of Array#shuffle in Ruby. Random.new(5) always returns the same sequence for rand, but shuffling is different:

Ruby 2.x:

[1,2,3].shuffle(random: Random.new(4))
=> [2, 1, 3]
[1,2,3].shuffle(random: Random.new(5))
=> [1, 2, 3]

Ruby 1.9.3:

[1,2,3].shuffle(random: Random.new(4))
=> [1, 2, 3]
[1,2,3].shuffle(random: Random.new(5))
=> [3, 2, 1]

And JRuby appears to be different again.

The implementation of Array#shuffle in Ruby 2.x differs from that used
in JRuby and Ruby 1.9.3. This seed happens to produce the same results
on both.

This is fragile in that it might break in future versions of Ruby. A
better solution would be to change the shuffle implementation to one
that is not implementation-specific.
@mattwynne mattwynne merged commit 2a6e1e1 into cucumber:master Apr 19, 2016
@mattwynne
Copy link
Member

Nice, tidy work @threedaymonk, thanks!

As part of a new policy and as recognition of the quality of your contribution, I've invited you as a committer to this repo. If you'd like to help out some more and don't know where to start, feel free to grab me on gitter.

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants