Run Django Tests from TextMate

It would be cool to be able to run my Django tests from within TextMate.

Update: this version will run just the tests from the active file, if there are any. Otherwise, it runs all of the tests in the whole project.

Here is a Command to do just that:

 1     #! /usr/bin/env ruby
 2     
 3     command = [ENV["TM_PYTHON"] || "python", "-u", "#{ENV['TM_PROJECT_DIRECTORY']}/manage.py", "test", "--noinput"]
 4     
 5     File.open(ENV['TM_FILEPATH']) do |f|
 6       f.readlines.each do |line|
 7         if line =~ /class (.*)\(.*TestCase\):/
 8           test_case = $1
 9           app_name = ENV['TM_FILEPATH'].split(ENV['TM_PROJECT_DIRECTORY'])[1].split('/')[1]
10           test_name = "#{app_name}.#{test_case}"
11           command << test_name
12         end
13       end
14     end
15     
16     require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
17     
18     ENV["PYTHONPATH"] = ENV["TM_BUNDLE_SUPPORT"] + (ENV.has_key?("PYTHONPATH") ? ":" + ENV["PYTHONPATH"] : "")
19     
20     TextMate::Executor.run(command) do |str, type|
21       if type == :err
22         if str =~ /\A[\.EF]*\Z/
23           str.gsub!(/(\.)/, "<span class=\"test ok\">\\1</span>")
24           str.gsub!(/(E|F)/, "<span class=\"test fail\">\\1</span>")
25           str + "<br/>\n"
26         elsif str =~ /\A(FAILED.*)\Z/
27           "<div class=\"test fail\">#{htmlize $1}</div>\n"
28         elsif str =~ /\A(OK.*)\Z/
29           "<div class=\"test ok\">#{htmlize $1}</div>\n"
30         elsif str =~ /^(\s+)File "(.+)", line (\d+), in (.*)/
31           indent = $1
32           file   = $2
33           line   = $3
34           method = $4
35           indent += " " if file.sub!(/^\"(.*)\"/,"\1")
36           url = "&url=file://" + e_url(file)
37           display_name = file.split('/').last 
38           "#{htmlize(indent)}<a class=\"near\" href=\"txmt://open?line=#{line + url}\">" +
39             (method ? "method #{method}" : "<em>at top level</em>") +
40             "</a> in <strong>#{display_name}</strong> at line #{line}<br/>\n"
41         end
42       end
43     end
blog comments powered by Disqus