Permalink
Browse files

Added test-runner script

  • Loading branch information...
radiantcapsule committed Jun 19, 2012
1 parent 539917e commit 5591fed09c126753114807f54e5472e9a62028c5
Showing with 92 additions and 0 deletions.
  1. +32 −0 README.md
  2. +60 −0 run-test
View
@@ -152,6 +152,38 @@ an attempt to cut down on the verbosity of your tests.
See the `uiautomation-ext.js` for details.
+# Running Tests
+
+Starting in iOS 5, Apple provided a way to run Instruments from the
+command-line. However, it's a bit fiddly and is very general-purpose
+so doing anything _useful_ with the output is kind of a pain.
+
+Tuneup now provides a Ruby script (run-test) to run your test scripts.
+The runner will parse the output of your test and produce a proper
+UNIX exit code based on whether or not your tests succeeded. It also
+provides some niceties like automatically specifying the full-path
+to your test script if you don't provide one.
+
+To use the runner, invoke it like so:
+
+ [path to tuneup]/run-test <app bundle> <test script> <output directory> [device_id]
+
+The `<app bundle>` argument is the name of your app (*.app for a real
+device, or the crazy path to the build bundle for the simulator). The
+`<test script>` argument specifies the JavaScript test file and the
+`<output directory>` is where the resulting Instruments output should
+go.
+
+If you provide the optional fourth argument, `device_id`, you can tell
+Instruments to run your test against a real device (identified by
+UDID). If this argument is not provided, the runner will assume you are
+running against the simulator.
+
+*NOTE*: If you are running tests with the simulator you need to provide
+a _fully-qualified_ path to the app bundle, which will be buried
+somewhere in `~/Library/Developer/Xcode/DerivedData`. For tests that
+execute against a real device, the name of the app bundle will suffice.
+
# Note on Patches/Pull Requests #
* Fork the project.
View
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby -wKU
+
+require "fileutils"
+require "pty"
+
+unless ARGV.length >= 3
+ STDERR.puts "USAGE: run-test-script <app bundle> <test script> <output directory> [device_id]"
+ STDERR.puts "NB: If no device_id is specified, the test will be run using the iOS Simulator"
+ exit 1
+end
+
+app_bundle, test_script, test_output, device_id = *ARGV
+
+# Instruments wants the test_script to be a fully-qualified path
+unless test_script[0] == "/"
+ test_script = "#{Dir.pwd}/#{test_script}"
+end
+
+SDKROOT = `/usr/bin/xcodebuild -version -sdk iphoneos | grep PlatformPath`.split(":")[1].chomp.sub(/^\s+/, "")
+
+command = ["/usr/bin/instruments"]
+command << "-w #{device_id}" unless device_id.nil?
+command << "-t #{SDKROOT}/Developer/Library/Instruments/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"
+command << app_bundle
+command << "-e UIASCRIPT #{test_script}"
+command << "-e UIARESULTSPATH #{test_output}"
+puts "command=#{command.join ' '}"
+
+failed = false
+
+unless File.directory? test_output
+ FileUtils.mkdir_p test_output
+end
+
+Dir.chdir(test_output) do
+ begin
+ PTY.spawn(command.join ' ') do |r, w, pid|
+ r.each do |line|
+ puts line
+ _, date, time, tz, type, msg = line.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) ([+-]\d{4}) ([^:]+): (.*)$/).to_a
+
+ case type
+ when "Fail"
+ failed = true
+ end
+ end
+ end
+ rescue Errno::EIO
+ rescue PTY::ChildExited => e
+ STDERR.puts "Instruments exited unexpectedly"
+ exit 1
+ end
+
+ if failed
+ STDERR.puts "#{test_script} failed, see log output for details"
+ exit 1
+ else
+ STDOUT.puts "TEST PASSED"
+ end
+end

0 comments on commit 5591fed

Please sign in to comment.