Last active
February 11, 2017 15:39
-
-
Save enkessler/6408879 to your computer and use it in GitHub Desktop.
An example usage of the cucumber_analytics, cuke_modeler, and other associated gems to create a lexicon of the steps used in a test suite.
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 'fileutils' | |
require 'cucumber_analytics' | |
require 'cuke_modeler' | |
require 'cql' | |
require 'cql/model_dsl' | |
# Project structure setup | |
your_location_here = "#{File.dirname(__FILE__)}/.." | |
feature_directory = "#{your_location_here}/features" | |
step_def_directory = "#{feature_directory}/step_definitions" | |
lexicon_directory = "#{your_location_here}/step_lexicon" | |
FileUtils.mkpath(lexicon_directory) | |
# Object collection | |
world = CucumberAnalytics::World | |
directory = CukeModeler::Directory.new(feature_directory) | |
glob = "#{step_def_directory}/**/*_steps.rb" | |
step_def_files = Dir.glob(glob) | |
steps = directory.query do | |
select 'steps' | |
from scenarios, outlines, backgrounds | |
end | |
steps = steps.collect { |step_result| step_result['steps'] }.flatten | |
# Analysis and output | |
step_def_files.each do |step_file| | |
world.clear_step_patterns | |
world.load_step_file(step_file) | |
step_defs = world.loaded_step_patterns | |
short_name = File.basename(step_file).sub(/\.rb$/, '') | |
lexicon_file_name = "#{lexicon_directory}/#{short_name}.feature" | |
File.open(lexicon_file_name, 'w') do |lexicon_feature_file| | |
feature = CukeModeler::Feature.new | |
feature.keyword = 'Feature' | |
feature.name = short_name.split('_').collect { |word| word.capitalize }.join(' ') | |
step_defs.each do |step_def| | |
scenario = CukeModeler::Scenario.new | |
scenario.keyword = 'Scenario' | |
scenario.name = step_def.inspect | |
pattern_usages = steps.select { |step| step.text =~ step_def } | |
pattern_usages.collect! { |usage| usage.text }.uniq! | |
if pattern_usages.empty? | |
scenario.description = 'This step is not used' | |
else | |
scenario.description = 'Unique usages of the step' | |
end | |
pattern_usages.each do |usage| | |
step = CukeModeler::Step.new | |
step.keyword = '*' | |
step.text = usage | |
scenario.steps << step | |
end | |
feature.tests << scenario | |
end | |
lexicon_feature_file.puts feature.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment