Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

runNosetestGUI.py

By: marksweb on Jul 12th, 2012  |  syntax: Python  |  size: 3.84 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python -d
  2.  
  3. import sys, os, re, subprocess, inspect
  4. from PyQt4 import QtCore, QtGui
  5. from etk.unittests.testconverter.scripts.nosetestsGUI import Ui_Dialog
  6.  
  7. class nosetestUI(QtGui.QMainWindow):
  8.     def __init__(self, parent=None):
  9.         self.localpath = os.getcwd()
  10.         self.localpath = os.path.abspath(os.path.join(self.localpath,
  11.                                             "..", "..", "..", ".."))
  12.         self.projectPath = 'main\\employer_toolkit\\app\\server\\etk\\unittests\\converted'
  13.  
  14.         QtGui.QWidget.__init__(self, parent)
  15.         self.processTests = self.getTestFiles()
  16.         self.ui = Ui_Dialog()
  17.         self.ui.setupUi(self)
  18.         self.filepath = ''
  19.  
  20.         for test in self.processTests.keys():
  21.             self.ui.selectProcess.addItem(test) # name
  22.  
  23.         self.ui.selectProcess.activated['QString'].connect(self.getTestProcess)
  24.         self.ui.selectTest.activated['QString'].connect(self.getTest)
  25.  
  26.         QtCore.QObject.connect(self.ui.testButton,
  27.           QtCore.SIGNAL("clicked()"), self.runTest)
  28.         QtCore.QObject.connect(self.ui.cancelButton,
  29.           QtCore.SIGNAL("clicked()"), self.close)
  30.  
  31.     def getTestFiles(self):
  32.         """ returns a dict of the unittest files in the converted folder, e.g.
  33.            fileDict[studentloan] = studentloan_unittest.py
  34.        """
  35.         fileDict = {}
  36.         path = os.path.join(self.localpath, self.projectPath)
  37.         results = []
  38.         results += [fname for fname in os.listdir(path) if fname.endswith('.py')]
  39.         results.remove('__init__.py')
  40.         for fname in results:
  41.             fileDict[re.sub('_unittest.py','',fname)] = fname
  42.         return fileDict
  43.  
  44.     def getTestProcess(self):
  45.         self.ui.errorMsg.setText('')
  46.         testProcess = unicode(self.ui.selectProcess.currentText())
  47.         tests = []  # Make sure the list is clear when the process changes
  48.         try:
  49.             self.filepath = os.path.join(self.localpath, self.projectPath,
  50.                                             self.processTests[testProcess])
  51.             print self.filepath
  52.         except KeyError:
  53.             self.ui.errorMsg.setText('Select a process')
  54.         with open(self.filepath) as f:
  55.             lines = f.readlines()
  56.             tests += [line.split(' ', 1)[1].split('(')[0] for line in lines
  57.                         if line.strip().startswith('def test_')]
  58.         self.ui.selectTest.clear()
  59.         self.ui.selectTest.addItem('All')
  60.         for test in tests:
  61.             test = re.sub('def','',test)
  62.             self.ui.selectTest.addItem(re.sub('\s','',test))
  63.  
  64.     def getTest(self):
  65.         test = unicode(self.ui.selectTest.currentText())
  66.         return test
  67.  
  68.     def runTest(self):
  69.         args = []
  70.         if self.ui.verbose.checkState() == 2: args.append('-v')
  71.         if self.ui.doctest.checkState() == 2: args.append('--with-doctest')
  72.         if self.ui.pyDebug.checkState() == 2: args.append('--pdb')
  73.         if self.ui.pyDebugFails.checkState() == 2: args.append('--pdb-failures')
  74.  
  75.         testProcess = unicode(self.ui.selectProcess.currentText())
  76.         test = self.getTest()
  77.         # Create the command to run the conversion
  78.         if test == "All":
  79.             cmd = "nosetests {process} {args}".format(process= self.filepath,
  80.                                                   args= ', '.join(args))
  81.         else:
  82.             cmd = "nosetests {process}:{test} {args}".format(process = self.filepath,
  83.                                                                 test= test, args= ' '.join(args))
  84.         print cmd
  85.         try:
  86.             # Run the converter tool
  87.             os.system(cmd)
  88.         except OSError, e:
  89.             raise Exception, "Error: ", e
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     app = QtGui.QApplication(sys.argv)
  94.     nosetest = nosetestUI()
  95.     nosetest.show()
  96.     sys.exit(app.exec_())