def testInit(self):
        """check if __init__ is correctly executed."""
        # test1
        # preconditions
        r = Report()  # => r.results is empty
        # do test
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        expectedColorList = [Qt.green, Qt.red, Qt.gray]
        self.assertTrue(dlg.resultColor == expectedColorList)
        self.assertTrue(dlg.resultsTree.topLevelItemCount() == 0)
        if isPyQt4:
            self.assertTrue(dlg.resultsTree.receivers(SIGNAL("itemClicked(QTreeWidgetItem *, int)")) == 1)
            self.assertTrue(dlg.resultsTree.receivers(SIGNAL("customContextMenuRequested(const QPoint &)")) == 1)
            self.assertTrue(dlg.buttonBox.receivers(SIGNAL("rejected()")) == 1)
        else:
            self.assertTrue(dlg.resultsTree.receivers(dlg.resultsTree.itemClicked) == 1)
            self.assertTrue(dlg.resultsTree.receivers(dlg.resultsTree.customContextMenuRequested) == 1)
            self.assertTrue(dlg.buttonBox.receivers(dlg.buttonBox.rejected) == 1)

        # test2
        # preconditions: populate with tests results
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        # do test
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        self.assertTrue(dlg.resultsTree.topLevelItemCount() == 1)
        self.assertTrue(dlg.resultsTree.topLevelItem(0).isExpanded())
        self.assertTrue(dlg.resultsTree.topLevelItem(0).childCount() == 3)
        self.assertTrue(dlg.resultsTree.topLevelItem(0).child(0).text(0) == "Functional test")
        self.assertTrue(dlg.resultsTree.topLevelItem(0).child(1).text(0) == "Test that fails")
        self.assertTrue(dlg.resultsTree.topLevelItem(0).child(2).text(0) == "Test that passes")
示例#2
0
 def testAddTestResult(self):
     """Test if a test is added to the results array"""
     r = Report()
     test = mock.Mock()
     tr = TestResult(test)
     r.addTestResult(tr)
     self.assertEqual(r.results[0], tr)
     self.assertEqual(len(r.results), 1)
 def testAddTestResult(self):
     """test if a test is added in the results array."""
     r = Report()
     test = mock.Mock()
     tr = TestResult(test)
     r.addTestResult(tr)
     self.assertEqual(r.results[0], tr)
     self.assertTrue(len(r.results) == 1)
 def runner(cls, suite):
     test = list(suite)[0]
     utw = UnitTestWrapper(test)
     report = Report()
     result = TestResult(test)
     step = utw.steps[0]
     try:
         step.function()
         result.passed()
     except Exception as e:
         result.failed(test, str(e))
     report.addTestResult(result)
     return report.results[0]
    def testShowPopupMenu(self):
        """check if a menu popup is opened if issue url is present."""
        # test1
        # preconditions
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        dlg.resultsTree.topLevelItem(0).child(1).setSelected(
            True)  # select 'Test that fails' that does NOT have url
        # do test
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        if isPyQt4:
            with mock.patch('PyQt4.QtGui.QMenu', qmenuMock):
                with mock.patch('PyQt4.QtGui.QAction', qactionMock):
                    dlg.showPopupMenu(point)
        else:
            with mock.patch('PyQt5.QtWidgets.QMenu', qmenuMock):
                with mock.patch('PyQt5.QtWidgets.QAction', qactionMock):
                    dlg.showPopupMenu(point)

        self.assertTrue(qmenuMock.mock_calls == [])
        self.assertTrue(qactionMock.mock_calls == [])

        # test2
        # preconditions
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        dlg.resultsTree.topLevelItem(0).child(0).setSelected(
            True)  # select 'Functional tests' that does have url
        # do test
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        with mock.patch('qgistester.reportdialog.QMenu', qmenuMock):
            with mock.patch('qgistester.reportdialog.QAction', qactionMock):
                self.assertEqual(
                    dlg.resultsTree.selectedItems()[0].result.test.issueUrl,
                    'http://www.example.com')
                dlg.showPopupMenu(point)
        self.assertIn('call()', str(qmenuMock.mock_calls[0]))
        self.assertIn('call().addAction', str(qmenuMock.mock_calls[1]))
        if isPyQt4:
            self.assertIn('call().exec_(PyQt4.QtCore.QPoint())',
                          str(qmenuMock.mock_calls[2]))
        else:
            self.assertIn('call().exec_(PyQt5.QtCore.QPoint())',
                          str(qmenuMock.mock_calls[2]))

        self.assertIn("call('Open issue page', None)",
                      str(qactionMock.mock_calls[0]))
        self.assertIn("call().triggered.connect",
                      str(qactionMock.mock_calls[1]))
    def testItemClicked(self):
        """Test that result is set to the clicked value"""
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)
        self.assertEqual(dlg.resultText.toPlainText(), '')
        dlg.itemClicked()
        self.assertEqual(dlg.resultText.toPlainText(), '')

        currentItem = dlg.resultsTree.topLevelItem(0).child(0)
        dlg.resultsTree.setCurrentItem(currentItem)
        dlg.itemClicked()
        self.assertIn('Test name: -Functional test',
                      dlg.resultText.toPlainText())
        self.assertIn('Test result:Test skipped', dlg.resultText.toPlainText())
 def testOkPressed(self):
     """Check that the widget is closed when OK clicked"""
     r = Report()
     dlg = ReportDialog(r)
     dlg.show()
     self.assertTrue(dlg.isVisible())
     dlg.close()
     self.assertFalse(dlg.isVisible())
 def testItemClicked(self):
     """test the result is set to the clicked value."""
     # preconditions
     r = Report()
     for test in self.allTests:
         tr = TestResult(test)
         r.addTestResult(tr)
     dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
     # do test1
     self.assertTrue(dlg.resultText.toPlainText() == "")
     dlg.itemClicked()
     self.assertTrue(dlg.resultText.toPlainText() == "")
     # do test 2
     currentItem = dlg.resultsTree.topLevelItem(0).child(0)
     dlg.resultsTree.setCurrentItem(currentItem)
     dlg.itemClicked()
     self.assertIn("Test name: -Functional test", dlg.resultText.toPlainText())
     self.assertIn("Test result:Test skipped", dlg.resultText.toPlainText())
示例#9
0
 def testOkPressed(self):
     """test the widget is closed."""
     # preconditions
     r = Report()
     dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
     dlg.show()
     # do test
     self.assertTrue(dlg.isVisible())
     dlg.close()
     self.assertFalse(dlg.isVisible())
示例#10
0
 def testItemClicked(self):
     """test the result is set to the clicked value."""
     # preconditions
     r = Report()
     for test in self.allTests:
         tr = TestResult(test)
         r.addTestResult(tr)
     dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
     # do test1
     self.assertTrue(dlg.resultText.toPlainText() == '')
     dlg.itemClicked()
     self.assertTrue(dlg.resultText.toPlainText() == '')
     # do test 2
     currentItem = dlg.resultsTree.topLevelItem(0).child(0)
     dlg.resultsTree.setCurrentItem(currentItem)
     dlg.itemClicked()
     self.assertIn('Test name: -Functional test',
                   dlg.resultText.toPlainText())
     self.assertIn('Test result:Test skipped', dlg.resultText.toPlainText())
    def testInit(self):
        """Check if __init__ is correctly executed"""
        r = Report()  # r.results is empty
        dlg = ReportDialog(r)
        expectedColorList = [
            Qt.green, Qt.red, Qt.gray, Qt.magenta,
            QColor(237, 189, 129)
        ]
        self.assertEqual(dlg.resultColor, expectedColorList)
        self.assertEqual(dlg.resultsTree.topLevelItemCount(), 0)
        self.assertEqual(
            dlg.resultsTree.receivers(dlg.resultsTree.itemClicked), 1)
        self.assertEqual(
            dlg.resultsTree.receivers(
                dlg.resultsTree.customContextMenuRequested), 1)
        self.assertEqual(dlg.buttonBox.receivers(dlg.buttonBox.rejected), 1)

        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)
        self.assertEqual(dlg.resultsTree.topLevelItemCount(), 1)
        self.assertTrue(dlg.resultsTree.topLevelItem(0).isExpanded())
        self.assertEqual(dlg.resultsTree.topLevelItem(0).childCount(), 3)
        self.assertEqual(
            dlg.resultsTree.topLevelItem(0).child(0).text(0),
            'Functional test')
        self.assertEqual(
            dlg.resultsTree.topLevelItem(0).child(1).text(0),
            'Test that fails')
        self.assertEqual(
            dlg.resultsTree.topLevelItem(0).child(2).text(0),
            'Test that passes')
    def testShowPopupMenu(self):
        """Check if a context menu is opened when issue url is present"""
        # check with 'Test that fails' item which does NOT have an url
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)
        dlg.resultsTree.topLevelItem(0).child(1).setSelected(True)
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        with mock.patch('PyQt5.QtWidgets.QMenu', qmenuMock):
            with mock.patch('PyQt5.QtWidgets.QAction', qactionMock):
                dlg.showPopupMenu(point)

        self.assertEqual(qmenuMock.mock_calls, [])
        self.assertEqual(qactionMock.mock_calls, [])

        # check with 'Functional tests' that does have an url
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)
        dlg.resultsTree.topLevelItem(0).child(0).setSelected(True)
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        with mock.patch('qgistester.reportdialog.QMenu', qmenuMock):
            with mock.patch('qgistester.reportdialog.QAction', qactionMock):
                self.assertEqual(
                    dlg.resultsTree.selectedItems()[0].result.test.issueUrl,
                    'http://www.example.com')
                dlg.showPopupMenu(point)
        self.assertIn('call()', str(qmenuMock.mock_calls[0]))
        self.assertIn('call().addAction', str(qmenuMock.mock_calls[1]))
        self.assertIn('call().exec_(PyQt5.QtCore.QPoint())',
                      str(qmenuMock.mock_calls[2]))
        self.assertIn("call('Open issue page', None)",
                      str(qactionMock.mock_calls[0]))
        self.assertIn("call().triggered.connect",
                      str(qactionMock.mock_calls[1]))
    def testShowPopupMenu(self):
        """check if a menu popup is opened if issue url is present."""
        # test1
        # preconditions
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        dlg.resultsTree.topLevelItem(0).child(1).setSelected(True)  # select 'Test that fails' that does NOT have url
        # do test
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        if isPyQt4:
            with mock.patch("PyQt4.QtGui.QMenu", qmenuMock):
                with mock.patch("PyQt4.QtGui.QAction", qactionMock):
                    dlg.showPopupMenu(point)
        else:
            with mock.patch("PyQt5.QtWidgets.QMenu", qmenuMock):
                with mock.patch("PyQt5.QtWidgets.QAction", qactionMock):
                    dlg.showPopupMenu(point)

        self.assertTrue(qmenuMock.mock_calls == [])
        self.assertTrue(qactionMock.mock_calls == [])

        # test2
        # preconditions
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        dlg.resultsTree.topLevelItem(0).child(0).setSelected(True)  # select 'Functional tests' that does have url
        # do test
        point = QPoint(0, 0)
        qmenuMock = mock.Mock(spec=QMenu)
        qactionMock = mock.Mock(spec=QAction)
        with mock.patch("qgistester.reportdialog.QMenu", qmenuMock):
            with mock.patch("qgistester.reportdialog.QAction", qactionMock):
                self.assertEqual(dlg.resultsTree.selectedItems()[0].result.test.issueUrl, "http://www.example.com")
                dlg.showPopupMenu(point)
        self.assertIn("call()", str(qmenuMock.mock_calls[0]))
        self.assertIn("call().addAction", str(qmenuMock.mock_calls[1]))
        if isPyQt4:
            self.assertIn("call().exec_(PyQt4.QtCore.QPoint())", str(qmenuMock.mock_calls[2]))
        else:
            self.assertIn("call().exec_(PyQt5.QtCore.QPoint())", str(qmenuMock.mock_calls[2]))

        self.assertIn("call('Open issue page', None)", str(qactionMock.mock_calls[0]))
        self.assertIn("call().triggered.connect", str(qactionMock.mock_calls[1]))
    def testInit(self):
        """check if __init__ is correctly executed."""
        # test1
        # preconditions
        r = Report()  # => r.results is empty
        # do test
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        expectedColorList = [Qt.green, Qt.red, Qt.gray]
        self.assertTrue(dlg.resultColor == expectedColorList)
        self.assertTrue(dlg.resultsTree.topLevelItemCount() == 0)
        if isPyQt4:
            self.assertTrue(
                dlg.resultsTree.receivers(
                    SIGNAL('itemClicked(QTreeWidgetItem *, int)')) == 1)
            self.assertTrue(
                dlg.resultsTree.receivers(
                    SIGNAL('customContextMenuRequested(const QPoint &)')) == 1)
            self.assertTrue(dlg.buttonBox.receivers(SIGNAL('rejected()')) == 1)
        else:
            self.assertTrue(
                dlg.resultsTree.receivers(dlg.resultsTree.itemClicked) == 1)
            self.assertTrue(
                dlg.resultsTree.receivers(
                    dlg.resultsTree.customContextMenuRequested) == 1)
            self.assertTrue(
                dlg.buttonBox.receivers(dlg.buttonBox.rejected) == 1)

        # test2
        # preconditions: populate with tests results
        r = Report()
        for test in self.allTests:
            tr = TestResult(test)
            r.addTestResult(tr)
        # do test
        dlg = ReportDialog(r)  # dlg.resultsTree is a QTreeWidget
        self.assertTrue(dlg.resultsTree.topLevelItemCount() == 1)
        self.assertTrue(dlg.resultsTree.topLevelItem(0).isExpanded())
        self.assertTrue(dlg.resultsTree.topLevelItem(0).childCount() == 3)
        self.assertTrue(
            dlg.resultsTree.topLevelItem(0).child(0).text(0) ==
            'Functional test')
        self.assertTrue(
            dlg.resultsTree.topLevelItem(0).child(1).text(0) ==
            'Test that fails')
        self.assertTrue(
            dlg.resultsTree.topLevelItem(0).child(2).text(0) ==
            'Test that passes')
示例#15
0
 def testInit(self):
     """check if __init__ is correctly executed."""
     r = Report()
     self.assertTrue(type(r.results) == list)
     self.assertTrue(len(r.results) == 0)
示例#16
0
 def startTesting(self):
     self.currentTest = 0
     self.report = Report()
     self.runNextTest()
示例#17
0
 def startTesting(self):
     self.currentTest = 0
     self.report = Report()
     self.runNextTest()
示例#18
0
class TesterWidget(BASE, WIDGET):

    currentTestResult = None
    currentTest = 0
    currentTestStep = 0

    BLINKING_INTERVAL = 1000

    buttonColors = ["", 'QPushButton {color: yellow;}']

    testingFinished = pyqtSignal()

    def __init__(self):
        super(TesterWidget, self).__init__()
        self.setupUi(self)
        self.setObjectName("TesterPluginPanel")
        self.btnCancel.clicked.connect(self.cancelTesting)
        self.btnTestOk.clicked.connect(self.testPasses)
        self.btnTestFailed.clicked.connect(self.testFails)
        self.btnRestartTest.clicked.connect(self.restartTest)
        self.btnSkip.clicked.connect(self.skipTest)
        self.btnNextStep.clicked.connect(self.runNextStep)
        self.buttons = [self.btnTestOk, self.btnTestFailed, self.btnNextStep]

        self.blinkTimer = QTimer()
        self.blinkTimer.timeout.connect(self._blink)

    def startBlinking(self):
        self.currentBlinkingTime = 0
        self.blinkTimer.start(self.BLINKING_INTERVAL)

    def stopBlinking(self):
        self.blinkTimer.stop()
        for button in self.buttons:
            button.setStyleSheet(self.buttonColors[0])

    def _blink(self):
        self.currentBlinkingTime += 1
        color = self.buttonColors[self.currentBlinkingTime % 2]
        for button in self.buttons:
            if button.isEnabled():
                button.setStyleSheet(color)

    def setTests(self, tests):
        self.tests = tests

    def startTesting(self):
        self.currentTest = 0
        self.report = Report()
        self.runNextTest()

    def getReportDialog(self):
        """Wrapper for easy mocking"""
        self.reportDialog = ReportDialog(self.report)
        return self.reportDialog

    def restartTest(self):
        self.currentTestResult = None
        self.runNextTest()

    def runNextTest(self):
        if self.currentTestResult:
            self.report.addTestResult(self.currentTestResult)
        if self.currentTest < len(self.tests):
            test = self.tests[self.currentTest]
            self.labelCurrentTest.setText("Current test: %s-%s" %
                                          (test.group.upper(), test.name))
            self.currentTestResult = TestResult(test)
            self.currentTestStep = 0
            self.runNextStep()
        else:
            QApplication.restoreOverrideCursor()
            self.testingFinished.emit()
            self.setVisible(False)

    def runNextStep(self):
        self.stopBlinking()
        test = self.tests[self.currentTest]
        step = test.steps[self.currentTestStep]
        self.btnSkip.setEnabled(True)
        self.btnCancel.setEnabled(True)
        if os.path.exists(step.description):
            with open(step.description) as f:
                html = "".join(f.readlines())
            self.webView.setHtml(html)
        else:
            if step.function is not None:
                self.webView.setHtml(
                    step.description +
                    "<p><b>[This is an automated step. Please, wait until it has been completed]</b></p>"
                )
            else:
                self.webView.setHtml(
                    step.description +
                    "<p><b>[Click on the right-hand side buttons once you have performed this step]</b></p>"
                )
        QCoreApplication.processEvents()
        if self.currentTestStep == len(test.steps) - 1:
            if step.function is not None:
                self.btnTestOk.setEnabled(False)
                self.btnTestFailed.setEnabled(False)
                self.btnNextStep.setEnabled(False)
                self.btnSkip.setEnabled(False)
                self.btnCancel.setEnabled(False)
                self.webView.setEnabled(False)
                QCoreApplication.processEvents()
                try:
                    execute(step.function)
                    self.testPasses()
                except Exception as e:
                    if isinstance(e, AssertionError):
                        self.testFails("%s\n%s" %
                                       (str(e), traceback.format_exc()))
                    else:
                        self.testContainsError(
                            "%s\n%s" % (str(e), traceback.format_exc()))
            else:
                self.btnTestOk.setEnabled(True)
                self.btnTestOk.setText("Test passes")
                self.btnTestFailed.setEnabled(True)
                self.btnTestFailed.setText("Test fails")
                self.webView.setEnabled(True)
                self.btnNextStep.setEnabled(False)
                if step.prestep:
                    try:
                        execute(step.prestep)
                    except Exception as e:
                        if isinstance(e, AssertionError):
                            self.testFailsAtSetup(
                                "%s\n%s" % (str(e), traceback.format_exc()))
                        else:
                            self.testContainsError(
                                "%s\n%s" % (str(e), traceback.format_exc()))
        else:
            if step.function is not None:
                self.btnTestOk.setEnabled(False)
                self.btnTestFailed.setEnabled(False)
                self.btnNextStep.setEnabled(False)
                self.btnSkip.setEnabled(False)
                self.btnCancel.setEnabled(False)
                self.webView.setEnabled(False)
                QCoreApplication.processEvents()
                try:
                    execute(step.function)
                    self.currentTestStep += 1
                    self.runNextStep()
                except Exception as e:
                    if isinstance(e, AssertionError):
                        self.testFails("%s\n%s" %
                                       (str(e), traceback.format_exc()))
                    else:
                        self.testContainsError(
                            "%s\n%s" % (str(e), traceback.format_exc()))
            else:
                self.currentTestStep += 1
                self.webView.setEnabled(True)
                self.btnNextStep.setEnabled(not step.isVerifyStep)
                if step.isVerifyStep:
                    self.btnTestOk.setEnabled(True)
                    self.btnTestOk.setText("Step passes")
                    self.btnTestFailed.setEnabled(True)
                    self.btnTestFailed.setText("Step fails")
                else:
                    self.btnTestOk.setEnabled(False)
                    self.btnTestFailed.setEnabled(False)
                if step.prestep:
                    try:
                        execute(step.prestep)
                    except Exception as e:
                        if isinstance(e, AssertionError):
                            self.testFailsAtSetup(
                                "%s\n%s" % (str(e), traceback.format_exc()))
                        else:
                            self.testContainsError(
                                "%s\n%s" % (str(e), traceback.format_exc()))
        if step.function is None:
            self.startBlinking()

    def testPasses(self):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text(
        ) == "Step passes":
            self.runNextStep()
        else:
            try:
                test = self.tests[self.currentTest]
                test.cleanup()
                self.currentTestResult.passed()
            except:
                self.currentTestResult.failed("Test cleanup",
                                              traceback.format_exc())

            self.currentTest += 1
            self.runNextTest()

    def testFails(self, msg=""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text(
        ) == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.failed(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest += 1
        self.runNextTest()

    def testFailsAtSetup(self, msg=""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text(
        ) == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.setupFailed(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest += 1
        self.runNextTest()

    def testContainsError(self, msg=""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text(
        ) == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.containsError(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest += 1
        self.runNextTest()

    def skipTest(self):
        try:
            test = self.tests[self.currentTest]
            test.cleanup()
        except:
            pass
        self.currentTest += 1
        self.currentTestResult.skipped()

        self.runNextTest()

    def cancelTesting(self):
        self.setVisible(False)
        self.testingFinished.emit()
示例#19
0
class TesterWidget(BASE, WIDGET):

    currentTestResult = None
    currentTest = 0
    currentTestStep = 0

    BLINKING_INTERVAL = 1000

    buttonColors = ["", 'QPushButton {color: yellow;}']

    testingFinished = pyqtSignal()


    def __init__(self):
        super(TesterWidget, self).__init__()
        self.setupUi(self)
        self.setObjectName("TesterPluginPanel")
        self.btnCancel.clicked.connect(self.cancelTesting)
        self.btnTestOk.clicked.connect(self.testPasses)
        self.btnTestFailed.clicked.connect(self.testFails)
        self.btnRestartTest.clicked.connect(self.restartTest)
        self.btnSkip.clicked.connect(self.skipTest)
        self.btnNextStep.clicked.connect(self.runNextStep)
        self.buttons = [self.btnTestOk, self.btnTestFailed, self.btnNextStep]

        self.blinkTimer = QTimer()
        self.blinkTimer.timeout.connect(self._blink)

    def startBlinking(self):
        self.currentBlinkingTime = 0
        self.blinkTimer.start(self.BLINKING_INTERVAL)

    def stopBlinking(self):
        self.blinkTimer.stop()
        for button in self.buttons:
            button.setStyleSheet(self.buttonColors[0])

    def _blink(self):
        self.currentBlinkingTime += 1
        color = self.buttonColors[self.currentBlinkingTime % 2]
        for button in self.buttons:
            if button.isEnabled():
                button.setStyleSheet(color)

    def setTests(self, tests):
        self.tests = tests

    def startTesting(self):
        self.currentTest = 0
        self.report = Report()
        self.runNextTest()

    def getReportDialog(self):
        """Wrapper for easy mocking"""
        self.reportDialog = ReportDialog(self.report)
        return self.reportDialog

    def restartTest(self):
        self.currentTestResult = None
        self.runNextTest()

    def runNextTest(self):
        if self.currentTestResult:
            self.report.addTestResult(self.currentTestResult)
        if self.currentTest < len(self.tests):
            test = self.tests[self.currentTest]
            self.labelCurrentTest.setText("Current test: %s-%s" % (test.group.upper(), test.name))
            self.currentTestResult = TestResult(test)
            self.currentTestStep = 0
            self.runNextStep()
        else:
            QApplication.restoreOverrideCursor()
            self.testingFinished.emit()
            self.setVisible(False)

    def runNextStep(self):
        self.stopBlinking()
        test = self.tests[self.currentTest]
        step = test.steps[self.currentTestStep]
        self.btnSkip.setEnabled(True)
        self.btnCancel.setEnabled(True)
        if os.path.exists(step.description):
            with open(step.description) as f:
                html = "".join(f.readlines())
            self.webView.setHtml(html)
        else:
            if step.function is not None:
                self.webView.setHtml(step.description + "<p><b>[This is an automated step. Please, wait until it has been completed]</b></p>")
            else:
                self.webView.setHtml(step.description + "<p><b>[Click on the right-hand side buttons once you have performed this step]</b></p>")
        QCoreApplication.processEvents()
        if self.currentTestStep == len(test.steps) - 1:
            if step.function is not None:
                self.btnTestOk.setEnabled(False)
                self.btnTestFailed.setEnabled(False)
                self.btnNextStep.setEnabled(False)
                self.btnSkip.setEnabled(False)
                self.btnCancel.setEnabled(False)
                self.webView.setEnabled(False)
                QCoreApplication.processEvents()
                try:
                    execute(step.function)
                    self.testPasses()
                except Exception as e:
                    if isinstance(e, AssertionError):
                        self.testFails("%s\n%s" % (str(e), traceback.format_exc()))
                    else:
                        self.testContainsError("%s\n%s" % (str(e), traceback.format_exc()))
            else:
                self.btnTestOk.setEnabled(True)
                self.btnTestOk.setText("Test passes")
                self.btnTestFailed.setEnabled(True)
                self.btnTestFailed.setText("Test fails")
                self.webView.setEnabled(True)
                self.btnNextStep.setEnabled(False)
                if step.prestep:
                    try:
                        execute(step.prestep)
                    except Exception as e:
                        if isinstance(e, AssertionError):
                            self.testFailsAtSetup("%s\n%s" % (str(e), traceback.format_exc()))
                        else:
                            self.testContainsError("%s\n%s" % (str(e), traceback.format_exc()))
        else:
            if step.function is not None:
                self.btnTestOk.setEnabled(False)
                self.btnTestFailed.setEnabled(False)
                self.btnNextStep.setEnabled(False)
                self.btnSkip.setEnabled(False)
                self.btnCancel.setEnabled(False)
                self.webView.setEnabled(False)
                QCoreApplication.processEvents()
                try:
                    execute(step.function)
                    self.currentTestStep += 1
                    self.runNextStep()
                except Exception as e:
                    if isinstance(e, AssertionError):
                        self.testFails("%s\n%s" % (str(e), traceback.format_exc()))
                    else:
                        self.testContainsError("%s\n%s" % (str(e), traceback.format_exc()))
            else:
                self.currentTestStep += 1
                self.webView.setEnabled(True)
                self.btnNextStep.setEnabled(not step.isVerifyStep)
                if step.isVerifyStep:
                    self.btnTestOk.setEnabled(True)
                    self.btnTestOk.setText("Step passes")
                    self.btnTestFailed.setEnabled(True)
                    self.btnTestFailed.setText("Step fails")
                else:
                    self.btnTestOk.setEnabled(False)
                    self.btnTestFailed.setEnabled(False)
                if step.prestep:
                    try:
                        execute(step.prestep)
                    except Exception as e:
                        if isinstance(e, AssertionError):
                            self.testFailsAtSetup("%s\n%s" % (str(e), traceback.format_exc()))
                        else:
                            self.testContainsError("%s\n%s" % (str(e), traceback.format_exc()))
        if step.function is None:
            self.startBlinking()

    def testPasses(self):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text() == "Step passes":
            self.runNextStep()
        else:
            try:
                test = self.tests[self.currentTest]
                test.cleanup()
                self.currentTestResult.passed()
            except:
                self.currentTestResult.failed("Test cleanup", traceback.format_exc())

            self.currentTest +=1
            self.runNextTest()


    def testFails(self, msg = ""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text() == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.failed(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest +=1
        self.runNextTest()

    def testFailsAtSetup(self, msg = ""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text() == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.setupFailed(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest +=1
        self.runNextTest()

    def testContainsError(self, msg = ""):
        test = self.tests[self.currentTest]
        if self.btnTestOk.isEnabled() and self.btnTestOk.text() == "Step passes":
            desc = test.steps[self.currentTestStep - 1].description
        else:
            desc = test.steps[self.currentTestStep].description
        self.currentTestResult.containsError(desc, msg)
        try:
            test.cleanup()
        except:
            pass
        self.currentTest +=1
        self.runNextTest()

    def skipTest(self):
        try:
            test = self.tests[self.currentTest]
            test.cleanup()
        except:
            pass
        self.currentTest +=1
        self.currentTestResult.skipped()

        self.runNextTest()

    def cancelTesting(self):
        self.setVisible(False)
        self.testingFinished.emit()
示例#20
0
 def testInit(self):
     """Check if __init__ is correctly executed"""
     r = Report()
     self.assertIsInstance(r.results, list)
     self.assertEqual(len(r.results), 0)