Skip to content

Instantly share code, notes, and snippets.

@wmbest2 wmbest2/test-runner
Created Mar 23, 2012

Embed
What would you like to do?
Python script to generate test commands
#! /usr/bin/python
import curses
import sys
import os
runners = ["android.test.InstrumenationTestRunner"
, "pl.polidea.instrumentation.PolideaInstrumentationTestRunner"]
create = ["Add class", "Run Tests"]
class Menu:
def __init__(self, screen):
self.screen = screen
def getStyle(self, pos, val):
if (pos == val):
return curses.color_pair(1)
return curses.A_NORMAL
def getMenuSelection(self, title, menuItems):
curses.init_pair(1,curses.COLOR_GREEN, curses.COLOR_WHITE)
pos = 0
while True:
self.screen.clear()
self.screen.border(0)
height = 6
height_offset = 0
self.screen.addstr(2, 2, title, curses.A_STANDOUT)
self.screen.addstr(4, 2, "Please select an option...", curses.A_BOLD)
for item in menuItems:
self.screen.addstr(height + (height_offset * 2),
4, str(height_offset + 1) + ". " + item,
self.getStyle(pos, height_offset + 1))
height_offset += 1
self.screen.addstr(height + (height_offset * 2),
4, str(height_offset + 1) + ". Exit",
self.getStyle(pos, height_offset + 1))
height_offset += 1
self.screen.refresh()
value = self.screen.getch()
if (value == curses.KEY_UP):
if (pos == 1):
pos = height_offset
else:
pos -= 1
elif (value == curses.KEY_DOWN):
if (pos == height_offset):
pos = 1
else:
pos += 1
elif (value == ord('\n')):
if pos != height_offset:
return pos
else:
break;
if (value >= ord('0') and value <= ord('9')):
pos = value - ord('0')
return pos
elif (value == ord('5')):
break
return None
def promptForValue(self, prompt):
self.screen.clear()
self.screen.border(0)
curses.echo()
output = prompt
self.screen.addstr(2, 2, output)
self.screen.refresh();
value = self.screen.getstr(4, 6, 60)
curses.noecho()
return value
class TestRunner:
test_runner = None
basePackage = None
testPackage = None
classes = []
def generateCommand(self):
out = "adb shell am instrument -w"
if len(self.classes) > 0:
out += " -e class "
for i, c in enumerate(self.classes):
if i != 0:
out += ","
out += self.basePackage + "." + c
out += " " + self.testPackage + "/" + self.test_runner
return out
def addClass(self):
c = self.Menu.promptForValue("Enter the classname with subpackage if necessary")
if c != "":
self.classes.append(c)
def __init__(self, screen):
self.screen = screen
self.Menu = Menu(self.screen)
def __call__(self, *args):
if self.test_runner is None:
runner = self.Menu.getMenuSelection("Choose TestRunner", runners) - 1
if runner == len(runners):
return None
self.test_runner = runners[runner]
if self.basePackage is None:
self.basePackage = self.Menu.promptForValue("Enter the package base")
testpack = self.Menu.promptForValue("Enter the test sub package")
self.testPackage = self.basePackage
if testpack != "":
self.testPackage += "." + testpack
self.classes = []
while True:
sel = self.Menu.getMenuSelection("Make a selection:", create)
if sel == 1:
self.addClass()
elif sel == 2:
return self.generateCommand()
else:
return None
def main():
test = TestRunner(curses.initscr())
while True:
val = curses.wrapper(test)
if val is None:
break
os.system(val)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.