Last active
August 24, 2020 07:56
This gist demonstrates one way to write a Cucumber scenario that tests an asynchronous feature. It is essentially a two part test but it maintains the documentation value of a single, cohesive test.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Testing an asynchronous thing | |
@async @batch_1 | |
Scenario: Some big batch process | |
Given some stuff is set up | |
When the thing happens | |
Then it worked | |
@async @batch_2 | |
Scenario: Another big batch process | |
Given some stuff is set up | |
When a different thing happens | |
Then it worked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async: -t @async |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
# Somehow figure out if the external processes have occurred. Using a flag file as an example. | |
Before('@async') do |scenario| | |
test_tags = scenario.source_tag_names | |
relevant_flag = test_tags.select { |tag| tag =~ /batch/ } | |
flags = File.open('flag_file.yml') { |file| YAML.load file } | |
@thing_has_happened = flags[relevant_flag] | |
end | |
# This hook just toggles the flag in order to demonstrate both code paths. Presumably some | |
# outside process would update the flag file once the triggered action is finished. | |
After('@async') do |scenario| | |
test_tags = scenario.source_tag_names | |
relevant_flag = test_tags.select { |tag| tag =~ /batch/ } | |
flag_hash = File.open('flag_file.yml') { |file| YAML.load file } | |
flag_hash[relevant_flag] = !@thing_has_happened | |
File.write('flag_file.yml', flag_hash.to_yaml) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
? - ! '@batch_1' | |
: false | |
? - ! '@batch_2' | |
: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given(/^some stuff is set up$/) do | |
# Do whatever you need to set things up | |
end | |
When(/^the thing happens$/) do | |
unless @thing_has_happened | |
# Possibly trigger something | |
# All done for now | |
pending | |
else | |
# Possibly do some more stuff | |
end | |
end | |
Then(/^it worked$/) do | |
# Check for success | |
end | |
When(/^a different thing happens$/) do | |
unless @thing_has_happened | |
# Possibly trigger something | |
# All done for now | |
pending | |
else | |
# Possibly do some more stuff | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment