rubytagscucumberacceptance-testingatdd

How to count tags on running scenarios in Ruby Cucumber?


I've a feature file with multiple scenarios and different tags for each of the scenarios. I'm running my Cucumber test using the rake command with a specific tag and am creating a custom HTML report.

The custom HTML report is created in a After hook. I am facing a problem as to how to get the count of the scenarios when I'm running with rake command. I use the

scenario.feature.feature_elements.size

to get the count of the total scenarios, but this gives the total scenarios count of the feature file and I'm trying to get only the scenarios count which are tagged with a specific tag.


Solution

  • In a Before hook, keep a count of each scenario's tags in a global as you run them:

    Before do |scenario|
       $tag_counts ||= {}
       scenario.tags.map(&:name).each do |tag|
         $tag_counts[tag] ||= 0
         $tag_counts[tag] += 1
       end
    end
    

    After all scenarios have run, you should be able to use the contents of the global in your custom reporter.