Skip to content

Instantly share code, notes, and snippets.

@enkessler
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enkessler/0042b9f0d3f7dce4eace to your computer and use it in GitHub Desktop.
Save enkessler/0042b9f0d3f7dce4eace to your computer and use it in GitHub Desktop.
Randomizing Cucumber outlines
Feature: Feature 1
Scenario: A normal test
* foo
@Random(50)
Scenario Outline:
* foo
Examples:
| values |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 0 |
Feature: Feature 2
Scenario Outline:
* foo
@Random(20)
Examples:
| values |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 0 |
@Random(100)
Examples:
| values |
| a |
| b |
require 'cucumber_analytics'
def choose_randomly(tag, rows)
percent = tag[/\d+/].to_i
percent /= 100.0
sample_size = (rows.count * percent).to_i
rows.sample(sample_size)
end
your_test_directory = Dir.pwd
world = CucumberAnalytics::World
directory = CucumberAnalytics::Directory.new(your_test_directory)
# Written for clarity, not efficiency. Could certainly use fewer collection iterations
all_tests = world.tests_in(directory)
all_outlines = all_tests.select { |test| test.is_a?(CucumberAnalytics::Outline) }
all_example_tables = all_outlines.collect { |outline| outline.examples }.flatten
randomized_example_tables = all_example_tables.select { |example| example.all_tags.any? { |tag| tag =~ /Random\(\d+\)/ } }
# Now that we've got all of the example rows, we need to randomly choose some of them to run
random_outline_rows = Array.new.tap { |rows|
randomized_example_tables.each do |example|
randomizer_tag = example.all_tags.select { |tag| tag =~ /Random\(\d+\)/ }.first # Assuming just one applicable randomizing tag
example_rows = example.row_elements
example_rows.shift # Need to get rid of the header row
rows << choose_randomly(randomizer_tag, example_rows)
end
}.flatten
# And then translate them into the familiar FILE:LINE form
final_runnable_tests = random_outline_rows.collect { |row| "#{row.get_ancestor(:feature_file).path}:#{row.source_line}" }
# Use them however you want at this point! Perhaps write them to a file and run them all?
File.open('my_random_tests.txt', 'w') { |file|
file.puts final_runnable_tests
}
system('cucumber @my_random_tests.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment