Description
By default junit executes test in default order that is based on method name hash code. In java hashcodes of string doesn't change between executions, so the order is random, but constant for given computer. That could lead to unpredictable errors.
$ groovysh
Groovy Shell (1.8.6, JVM: 1.7.0_79)
Type 'help' or '\h' for help.
---------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> a = 'testSample'
===> testSample
groovy:000> a.hashCode()
===> 1710059740
groovy:000>
$ groovysh
Groovy Shell (1.8.6, JVM: 1.7.0_79)
Type 'help' or '\h' for help.
---------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> a = 'testSample'
===> testSample
groovy:000> a.hashCode()
===> 1710059740
groovy:000>
To fix this enchantment, there should be truly random order option to explicitly specify in test case using @FixMethodOrder
.
I propose to follow Rspec --order
random option implementation. It actually makes random order for execution but seed used for this is displayed and logged into reports. This gives developers a chance to execute the same execution with --order rand:3455
. It guarantees that tests will be executed in the same order, therefore it's possible to hunt for execution order bugs.
Cite:
When you use --order random, RSpec prints out the random number it used to seed the randomizer. When you think you’ve found an order-dependency bug, you can pass the seed along and the order will remain consistent:
--order rand:3455
Example execution:
$ rspec test.rb --order rand
Randomized with seed 64539
testing
should equal 5
should contain 'test'
Finished in 0.02 seconds (files took 0.0092 seconds to load)
2 examples, 0 failures
Randomized with seed 64539
$ rspec test.rb --order rand
Randomized with seed 12834
testing
should contain 'test'
should equal 5
Finished in 0.02 seconds (files took 0.0088 seconds to load)
2 examples, 0 failures
Randomized with seed 12834
$ rspec test.rb --order rand:12834
Randomized with seed 12834
testing
should contain 'test'
should equal 5
Finished in 0.02 seconds (files took 0.0083 seconds to load)
2 examples, 0 failures
Randomized with seed 12834
Rspec random refenrence: http://blog.davidchelimsky.net/blog/2012/01/04/rspec-28-is-released/