Skip to content

Instantly share code, notes, and snippets.

@jonfuller
Created September 23, 2009 20:00
Show Gist options
  • Save jonfuller/192242 to your computer and use it in GitHub Desktop.
Save jonfuller/192242 to your computer and use it in GitHub Desktop.
Corresponding autotest file for the watchr script
require 'rexml/document'
def build(test_project)
`msbuild /nologo #{test_project}`
end
def mstest(test_container, test_results_file, tests_to_run)
tests_to_run = ([""] << tests_to_run).flatten
File.delete(test_results_file) if File.exists?(test_results_file)
`mstest /nologo /resultsfile:#{test_results_file} /testcontainer:#{test_container} #{tests_to_run.join(" /test:")}`
test_results = process_mstest_results(test_results_file)
File.delete(test_results_file) if File.exists?(test_results_file)
return test_results
end
def process_mstest_results(results_file)
results = {}
File.open(results_file) do |file|
xml = REXML::Document.new(file)
results[:num_tests] = xml.get_elements("//UnitTestResult").length
failures = []
xml.elements.each("//UnitTestResult[@outcome='Failed']") do |e|
failure = {}
failure[:message] = e.elements["Output/ErrorInfo/Message"].get_text
stack = e.elements["Output/ErrorInfo/StackTrace"].get_text.value
stack_match = /^.*at (.*) in(.*):line (\d+)$/.match(stack)
failure[:stack] = stack_match[1] if stack_match
failure[:location] = stack_match[2] if stack_match
failure[:line] = stack_match[3] if stack_match
failure[:stack] = stack if !stack_match
failures << failure
end
results[:failures] = failures
end
return results
end
def show_results(results)
puts "#{results[:num_tests]} tests run (#{results[:failures].length} failures)"
results[:failures].each do |failure|
puts "---------------------------------------"
puts "Message: #{failure[:message]}"
puts "Location: #{failure[:location]}"
puts "Line: #{failure[:line]}"
puts "Stack Trace: #{failure[:stack]}"
end
end
def run_test(file_name)
test_container = "KvlApplication/UnitTests/bin/Debug/Motorola.Kvl.UnitTests.dll"
test_results_file = "result.trx"
test_project = "KvlApplication/UnitTests/UnitTests.csproj"
system("cls")
system("echo Detected change in:")
system("echo #{file_name}")
system("echo Building and Testing")
test_namespace = ''
test_class = ''
test_names = []
File.open(file_name, "r") do |f|
f.each do |line|
ns_match = /^namespace (.*)$/.match(line)
test_namespace = ns_match[1] if ns_match
class_match = /^\s*public class (.\w*).*$/.match(line)
test_class = class_match[1] if class_match
test_name_match = /^\s*public void (\w*).*$/.match(line)
test_names << test_name_match[1] if test_name_match
end
end
test_names = test_names.map { |n| "#{test_namespace}.#{test_class}.#{n}" }
build(test_project)
results = mstest(test_container, test_results_file, test_names)
show_results(results)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment