Skip to content

Instantly share code, notes, and snippets.

Last active August 24, 2021 16:59
What would you like to do?
Script to generate a list of rails test cases by folder
#!/usr/bin/env ruby
# A script to generate a list of tests within a rails project and output it as a csv
# Usage: ruby list_rails_tests.rb [output_file_path]
# Author: Matt Decuir
# Last Updated: 8/24/2021
# This scrips assumes you're running it from the project root
require 'csv'
# grep_output = `cd #{File.expand_path('../../../', __FILE__)} && grep -nr -e 'def test' -e 'Scenario.*:' -e '^\\s*should\\b' -e '^\\s*test.*do\\b' test`
grep_output = `grep -nr -e 'def test' -e 'Scenario.*:' -e '^\\s*should\\b' -e '^\\s*test.*do\\b' test`
column_names = ['Testing Library','Testing Syntax','Directory','File Name','Line Number','Test Name']
output_file = "tests_#{'%Y%m%d%H%M')}.csv"
output_directory = ARGV[0] || Dir.pwd + '/'
output_path = File.expand_path("#{output_directory}#{output_file}", __FILE__), 'wb') do |csv|
csv << column_names
grep_output.lines.each do |line|
row = line.split(':',3).map(&:strip)
directory = File.dirname row[0]
file_name = File.basename row[0]
line_num = row[1]
test_name = row[2]
# map library and syntax
case test_name
when /def test/
library = 'MiniTest'
syntax = 'def test'
when /Scenario.*:/
library = 'Cucumber'
syntax = 'Scenario'
when /test.*do$/
library = 'MiniTest'
syntax = 'test do'
when /should/
library = 'Shoulda'
syntax = 'should'
library = 'Other'
syntax = 'Other'
csv << [library, syntax, directory, file_name, line_num, test_name]
puts "#{output_file} saved to #{output_directory}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment