Beispiel #1
0
 def test_failfastAddFailure(self):
     """
     addFailure triggers failfast when it is set
     """
     self.args.failfast = True
     gtr = GreenTestResult(self.args, GreenStream(self.stream))
     self.assertEqual(gtr.failfast, True)
     try:
         raise Exception
     except:
         err = sys.exc_info()
     self.assertEqual(gtr.shouldStop, False)
     gtr.addFailure(MyProtoTest(), proto_error(err))
     self.assertEqual(gtr.shouldStop, True)
Beispiel #2
0
 def test_failfastAddFailure(self):
     """
     addFailure triggers failfast when it is set
     """
     self.args.failfast = True
     gtr = GreenTestResult(self.args, GreenStream(self.stream))
     self.assertEqual(gtr.failfast, True)
     try:
         raise Exception
     except:
         err = sys.exc_info()
     self.assertEqual(gtr.shouldStop, False)
     gtr.addFailure(MyProtoTest(), proto_error(err))
     self.assertEqual(gtr.shouldStop, True)
Beispiel #3
0
class JUnitXMLReportIsGenerated(TestCase):
    def setUp(self):
        self._destination = StringIO()
        self._test_results = GreenTestResult(default_args,
                                             GreenStream(StringIO()))
        self._adapter = JUnitXML()

        self._test = ProtoTest()
        self._test.module = "my_module"
        self._test.class_name = "MyClass"
        self._test.method_name = "my_method"

    def test_when_the_results_contain_only_one_successful_test(self):
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.PASSED
                    }
                }
            }
        })

    def test_when_the_results_contain_tests_with_various_verdict(self):
        self._test_results.addSuccess(
            test("my.module", "MyClass", "test_method1"))
        self._test_results.addSuccess(
            test("my.module", "MyClass", "test_method2"))
        self._record_failure(test("my.module", "MyClass", "test_method3"))
        self._record_failure(test("my.module", "MyClass", "test_method4"))
        self._record_error(test("my.module", "MyClass", "test_method5"))
        self._test_results.addSkip(
            test("my.module", "MyClass", "test_method6"), "Take too long")

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my.module.MyClass": {
                "#tests": "6",
                "#failures": "2",
                "#errors": "1",
                "#skipped": "1",
                "tests": {
                    "test_method1": {
                        "verdict": Verdict.PASSED
                    },
                    "test_method2": {
                        "verdict": Verdict.PASSED
                    },
                    "test_method3": {
                        "verdict": Verdict.FAILED
                    },
                    "test_method4": {
                        "verdict": Verdict.FAILED
                    },
                    "test_method5": {
                        "verdict": Verdict.ERROR
                    },
                    "test_method6": {
                        "verdict": Verdict.SKIPPED
                    }
                }
            },
        })

    def _record_failure(self, test):
        try:
            raise ValueError("Wrong value")
        except:
            error = proto_error(exc_info())
        self._test_results.addFailure(test, error)

    def _record_error(self, test):
        try:
            raise ValueError("Wrong value")
        except:
            error = proto_error(exc_info())
        self._test_results.addError(test, error)

    def test_when_the_results_contain_only_one_test_with_output(self):
        output = "This is the output of the test"
        self._test_results.recordStdout(self._test, output)
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.PASSED,
                        "stdout": output
                    }
                }
            }
        })

    def test_when_the_results_contain_only_one_test_with_errput(self):
        errput = "This is the errput of the test"
        self._test_results.recordStderr(self._test, errput)
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.PASSED,
                        "stderr": errput
                    }
                }
            }
        })

    def test_when_the_results_contain_only_one_failed_test(self):
        self._record_failure(test("my_module", "MyClass", "my_method"))

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.FAILED
                    }
                }
            }
        })

    def test_when_the_results_contain_only_one_erroneous_test(self):
        self._record_error(test("my_module", "MyClass", "my_method"))

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.ERROR
                    }
                }
            }
        })

    def test_when_the_results_contain_only_one_skipped_test(self):
        self._test_results.addSkip(self._test, "reason for skipping")

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.SKIPPED
                    }
                }
            }
        })

    def _assert_report_is(self, report):
        """
        Verify the structure of the generated XML text against the given
        'report' structure.
        """
        root = from_xml(self._destination.getvalue())
        test_suites = root.findall(JUnitDialect.TEST_SUITE)
        self.assertEqual(len(report), len(test_suites))
        for each_suite in test_suites:
            self._assert_suite(report, each_suite)

    def _assert_suite(self, expected_report, suite):
        """
        Verify that the given 'suite' matches one in the expected test report.
        """
        name = suite.get(JUnitDialect.NAME)
        self.assertIsNotNone(name)
        self.assertIn(name, expected_report)
        expected_suite = expected_report[name]

        # Check the count of tests
        if "#tests" in expected_suite:
            self.assertEqual(expected_suite["#tests"],
                             suite.get(JUnitDialect.TEST_COUNT))

        # Check the count of failures
        if "#failures" in expected_suite:
            self.assertEqual(expected_suite["#failures"],
                             suite.get(JUnitDialect.FAILURE_COUNT))

        # Check the count of errors
        if "#errors" in expected_suite:
            self.assertEqual(expected_suite["#errors"],
                             suite.get(JUnitDialect.ERROR_COUNT))

        # Check the count of skipped tests
        if "#skipped" in expected_suite:
            self.assertEqual(expected_suite["#skipped"],
                             suite.get(JUnitDialect.SKIPPED_COUNT))

        # Check individual test reports
        self.assertEqual(len(expected_suite["tests"]), len(suite))
        for each_test in suite:
            self._assert_test(expected_suite["tests"], each_test)

    def _assert_test(self, expected_suite, test):
        """
        Verify that the given 'test' matches one in the expected test suite.
        """
        name = test.get(JUnitDialect.NAME)
        self.assertIsNotNone(test)
        self.assertIn(name, expected_suite)
        expected_test = expected_suite[name]

        test_passed = True

        for key, expected in expected_test.items():
            if key == "verdict":
                self._assert_verdict(expected, test)

            elif key == "stdout":
                system_out = test.find(JUnitDialect.SYSTEM_OUT)
                self.assertIsNotNone(system_out)
                self.assertEqual(expected, system_out.text)

            elif key == "stderr":
                system_err = test.find(JUnitDialect.SYSTEM_ERR)
                self.assertIsNotNone(system_err)
                self.assertEqual(expected, system_err.text)

    def _assert_verdict(self, expected_verdict, test):
        failure = test.find(JUnitDialect.FAILURE)
        error = test.find(JUnitDialect.ERROR)
        skipped = test.find(JUnitDialect.SKIPPED)

        if expected_verdict == Verdict.FAILED:
            self.assertIsNotNone(failure)
            self.assertIsNone(error)
            self.assertIsNone(skipped)

        elif expected_verdict == Verdict.ERROR:
            self.assertIsNone(failure)
            self.assertIsNotNone(error)
            self.assertIsNone(skipped)

        elif expected_verdict == Verdict.SKIPPED:
            self.assertIsNone(failure)
            self.assertIsNone(error)
            self.assertIsNotNone(skipped)

        else:  # Verdict == PASSED
            self.assertIsNone(failure)
            self.assertIsNone(error)
            self.assertIsNone(skipped)
Beispiel #4
0
class TestGreenTestResultAdds(unittest.TestCase):

    def setUp(self):
        self.stream = StringIO()
        self.args = copy.deepcopy(default_args)
        self.args.verbose = 0
        self.gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.gtr._reportOutcome = MagicMock()

    def tearDown(self):
        del(self.stream)
        del(self.gtr)

    def test_addSuccess(self):
        """
        addSuccess() makes the correct calls to other functions.
        """
        test = MagicMock()
        test.shortDescription.return_value = 'a'
        test.__str__.return_value = 'b'
        test = proto_test(test)
        self.gtr.addSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, '.', self.gtr.colors.passing)

    def test_addError(self):
        """
        addError() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addError(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'E', self.gtr.colors.error, err)

    def test_addFailure(self):
        """
        addFailure() makes the correct calls to other functions.
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'F', self.gtr.colors.failing, err)

    def test_addFailureTwistedSkip(self):
        """
        Twisted's practice of calling addFailure() with their skips is detected
        and redirected to addSkip()
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        reason = "Twisted is odd"
        err = proto_error(err)
        err.traceback_lines = ["UnsupportedTrialFeature: ('skip', '{}')"
                               .format(reason)]
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 's', self.gtr.colors.skipped, reason=reason)

    def test_addSkip(self):
        """
        addSkip() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        reason = 'skip reason'
        self.gtr.addSkip(test, reason)
        self.gtr._reportOutcome.assert_called_with(
                test, 's', self.gtr.colors.skipped, reason=reason)

    def test_addExpectedFailure(self):
        """
        addExpectedFailure() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addExpectedFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'x', self.gtr.colors.expectedFailure, err)

    def test_addUnexpectedSuccess(self):
        """
        addUnexpectedSuccess() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        self.gtr.addUnexpectedSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, 'u', self.gtr.colors.unexpectedSuccess)

    def test_wasSuccessful(self):
        """
        wasSuccessful returns what we expect.
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.assertEqual(gtr.wasSuccessful(), False)
        gtr.passing.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)
        gtr.all_errors.append('anything')
        self.assertEqual(gtr.wasSuccessful(), False)

    def test_wasSuccessful_expectedFailures(self):
        """
        wasSuccessful returns what we expect when we only have expectedFailures
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.expectedFailures.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_passing(self):
        """
        wasSuccessful returns what we expect when we only have passing tests
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.passing.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_skipped(self):
        """
        wasSuccessful returns what we expect when we only have skipped tests
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.skipped.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_unexpectedSuccesses(self):
        """
        wasSuccessful returns what we expect when we only have unexpectedSuccesses
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.unexpectedSuccesses.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)
Beispiel #5
0
class TestGreenTestResultAdds(unittest.TestCase):


    def setUp(self):
        self.stream = StringIO()
        self.gtr = GreenTestResult(GreenStream(self.stream), None, 0)
        self.gtr._reportOutcome = MagicMock()


    def tearDown(self):
        del(self.stream)
        del(self.gtr)


    def test_addSuccess(self):
        "addSuccess() makes the correct calls to other functions."
        test = MagicMock()
        test.shortDescription.return_value = 'a'
        test.__str__.return_value = 'b'
        test = proto_test(test)
        self.gtr.addSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, '.', self.gtr.colors.passing)


    def test_addError(self):
        "addError() makes the correct calls to other functions."
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addError(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'E', self.gtr.colors.error, err)


    def test_addFailure(self):
        "addFailure() makes the correct calls to other functions."
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'F', self.gtr.colors.failing, err)


    def test_addSkip(self):
        "addSkip() makes the correct calls to other functions."
        test = proto_test(MagicMock())
        reason = 'skip reason'
        self.gtr.addSkip(test, reason)
        self.gtr._reportOutcome.assert_called_with(
                test, 's', self.gtr.colors.skipped, reason=reason)


    def test_addExpectedFailure(self):
        "addExpectedFailure() makes the correct calls to other functions."
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addExpectedFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'x', self.gtr.colors.expectedFailure, err)


    def test_addUnexpectedSuccess(self):
        "addUnexpectedSuccess() makes the correct calls to other functions."
        test = proto_test(MagicMock())
        self.gtr.addUnexpectedSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, 'u', self.gtr.colors.unexpectedSuccess)


    def test_wasSuccessful(self):
        "wasSuccessful returns what we expect"
        gtr = GreenTestResult(GreenStream(self.stream), None, 1)
        self.assertEqual(gtr.wasSuccessful(), True)
        gtr.all_errors.append('anything')
        self.assertEqual(gtr.wasSuccessful(), False)
Beispiel #6
0
class TestGreenTestResultAdds(unittest.TestCase):

    def setUp(self):
        self.stream = StringIO()
        self.args = copy.deepcopy(default_args)
        self.args.verbose = 0
        self.gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.gtr._reportOutcome = MagicMock()

    def tearDown(self):
        del(self.stream)
        del(self.gtr)

    def test_addSuccess(self):
        """
        addSuccess() makes the correct calls to other functions.
        """
        test = MagicMock()
        test.shortDescription.return_value = 'a'
        test.__str__.return_value = 'b'
        test = proto_test(test)
        self.gtr.addSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, '.', self.gtr.colors.passing)

    def test_addError(self):
        """
        addError() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addError(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'E', self.gtr.colors.error, err)

    def test_addFailure(self):
        """
        addFailure() makes the correct calls to other functions.
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'F', self.gtr.colors.failing, err)

    def test_addFailureTwistedSkip(self):
        """
        Twisted's practice of calling addFailure() with their skips is detected
        and redirected to addSkip()
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        reason = "Twisted is odd"
        err = proto_error(err)
        err.traceback_lines = ["UnsupportedTrialFeature: ('skip', '{}')"
                               .format(reason)]
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 's', self.gtr.colors.skipped, reason=reason)

    def test_addSkip(self):
        """
        addSkip() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        reason = 'skip reason'
        self.gtr.addSkip(test, reason)
        self.gtr._reportOutcome.assert_called_with(
                test, 's', self.gtr.colors.skipped, reason=reason)

    def test_addExpectedFailure(self):
        """
        addExpectedFailure() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addExpectedFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
                test, 'x', self.gtr.colors.expectedFailure, err)

    def test_addUnexpectedSuccess(self):
        """
        addUnexpectedSuccess() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        self.gtr.addUnexpectedSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
                test, 'u', self.gtr.colors.unexpectedSuccess)

    def test_wasSuccessful(self):
        """
        wasSuccessful returns what we expect.
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.assertEqual(gtr.wasSuccessful(), False)
        gtr.passing.append('anything')
        self.assertEqual(gtr.wasSuccessful(), True)
        gtr.all_errors.append('anything')
        self.assertEqual(gtr.wasSuccessful(), False)
Beispiel #7
0
class JUnitXMLReportIsGenerated(TestCase):


    def setUp(self):
        self._destination = StringIO()
        self._test_results = GreenTestResult(default_args,
                                             GreenStream(StringIO()))
        self._adapter = JUnitXML()

        self._test = ProtoTest()
        self._test.module      = "my_module"
        self._test.class_name  = "MyClass"
        self._test.method_name = "my_method"



    def test_when_the_results_contain_only_one_successful_test(self):
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {"verdict": Verdict.PASSED}
                }
            }
        })


    def test_when_the_results_contain_tests_with_various_verdict(self):
        self._test_results.addSuccess(
            test("my.module", "MyClass", "test_method1"))
        self._test_results.addSuccess(
            test("my.module", "MyClass", "test_method2"))
        self._record_failure(
            test("my.module", "MyClass", "test_method3"))
        self._record_failure(
            test("my.module", "MyClass", "test_method4"))
        self._record_error(
            test("my.module", "MyClass", "test_method5"))
        self._test_results.addSkip(
            test("my.module", "MyClass", "test_method6"),
            "Take too long")

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my.module.MyClass": {
                "#tests": "6",
                "#failures": "2",
                "#errors": "1",
                "#skipped": "1",
                "tests": {
                    "test_method1": { "verdict": Verdict.PASSED },
                    "test_method2": { "verdict": Verdict.PASSED },
                    "test_method3": { "verdict": Verdict.FAILED },
                    "test_method4": { "verdict": Verdict.FAILED },
                    "test_method5": { "verdict": Verdict.ERROR },
                    "test_method6": { "verdict": Verdict.SKIPPED }
                }
            },
        })


    def _record_failure(self, test):
        try:
            raise ValueError("Wrong value")
        except:
            error = proto_error(exc_info())
        self._test_results.addFailure(test, error)


    def _record_error(self, test):
        try:
            raise ValueError("Wrong value")
        except:
            error = proto_error(exc_info())
        self._test_results.addError(test, error)



    def test_when_the_results_contain_only_one_test_with_output(self):
        output = "This is the output of the test"
        self._test_results.recordStdout(self._test, output)
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.PASSED,
                        "stdout": output
                    }
                }
            }
        })


    def test_when_the_results_contain_only_one_test_with_errput(self):
        errput = "This is the errput of the test"
        self._test_results.recordStderr(self._test, errput)
        self._test_results.addSuccess(self._test)

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {
                        "verdict": Verdict.PASSED,
                        "stderr": errput
                    }
                }
            }
        })



    def test_when_the_results_contain_only_one_failed_test(self):
        self._record_failure(
            test("my_module", "MyClass", "my_method"))

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests" : {
                    "my_method": {"verdict": Verdict.FAILED}
                }
            }
        })


    def test_when_the_results_contain_only_one_erroneous_test(self):
        self._record_error(
            test("my_module", "MyClass", "my_method"))

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {"verdict": Verdict.ERROR}
                }
            }
        })


    def test_when_the_results_contain_only_one_skipped_test(self):
        self._test_results.addSkip(self._test, "reason for skipping")

        self._adapter.save_as(self._test_results, self._destination)

        self._assert_report_is({
            "my_module.MyClass": {
                "tests": {
                    "my_method": {"verdict": Verdict.SKIPPED}
                }
            }
        })


    def _assert_report_is(self, report):
        """
        Verify the structure of the generated XML text against the given
        'report' structure.
        """
        root = from_xml(self._destination.getvalue())
        test_suites = root.findall(JUnitDialect.TEST_SUITE)
        self.assertEqual(len(report), len(test_suites))
        for each_suite in test_suites:
            self._assert_suite(report, each_suite)


    def _assert_suite(self, expected_report, suite):
        """
        Verify that the given 'suite' matches one in the expected test report.
        """
        name = suite.get(JUnitDialect.NAME)
        self.assertIsNotNone(name)
        self.assertIn(name, expected_report)
        expected_suite = expected_report[name]

        # Check the count of tests
        if "#tests" in expected_suite:
            self.assertEqual(expected_suite["#tests"],
                             suite.get(JUnitDialect.TEST_COUNT))

        # Check the count of failures
        if "#failures" in expected_suite:
            self.assertEqual(expected_suite["#failures"],
                             suite.get(JUnitDialect.FAILURE_COUNT))

        # Check the count of errors
        if "#errors" in expected_suite:
            self.assertEqual(expected_suite["#errors"],
                             suite.get(JUnitDialect.ERROR_COUNT))

        # Check the count of skipped tests
        if "#skipped" in expected_suite:
            self.assertEqual(expected_suite["#skipped"],
                             suite.get(JUnitDialect.SKIPPED_COUNT))

        # Check individual test reports
        self.assertEqual(len(expected_suite["tests"]), len(suite))
        for each_test in suite:
            self._assert_test(expected_suite["tests"], each_test)


    def _assert_test(self, expected_suite, test):
        """
        Verify that the given 'test' matches one in the expected test suite.
        """
        name = test.get(JUnitDialect.NAME)
        self.assertIsNotNone(test)
        self.assertIn(name, expected_suite)
        expected_test = expected_suite[name]

        test_passed = True

        for key, expected in expected_test.items():
            if key == "verdict":
                self._assert_verdict(expected, test)

            elif key == "stdout":
                system_out = test.find(JUnitDialect.SYSTEM_OUT)
                self.assertIsNotNone(system_out)
                self.assertEqual(expected, system_out.text)

            elif key == "stderr":
                system_err = test.find(JUnitDialect.SYSTEM_ERR)
                self.assertIsNotNone(system_err)
                self.assertEqual(expected, system_err.text)



    def _assert_verdict(self, expected_verdict, test):
        failure = test.find(JUnitDialect.FAILURE)
        error = test.find(JUnitDialect.ERROR)
        skipped = test.find(JUnitDialect.SKIPPED)

        if expected_verdict == Verdict.FAILED:
            self.assertIsNotNone(failure)
            self.assertIsNone(error)
            self.assertIsNone(skipped)

        elif expected_verdict == Verdict.ERROR:
            self.assertIsNone(failure)
            self.assertIsNotNone(error)
            self.assertIsNone(skipped)

        elif expected_verdict == Verdict.SKIPPED:
            self.assertIsNone(failure)
            self.assertIsNone(error)
            self.assertIsNotNone(skipped)

        else: # Verdict == PASSED
            self.assertIsNone(failure)
            self.assertIsNone(error)
            self.assertIsNone(skipped)
Beispiel #8
0
class TestGreenTestResultAdds(unittest.TestCase):
    def setUp(self):
        self.stream = StringIO()
        self.args = copy.deepcopy(default_args)
        self.args.verbose = 0
        self.gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.gtr._reportOutcome = MagicMock()

    def tearDown(self):
        del self.stream
        del self.gtr

    def test_addSuccess(self):
        """
        addSuccess() makes the correct calls to other functions.
        """
        test = MagicMock()
        test.shortDescription.return_value = "a"
        test.__str__.return_value = "b"
        test = proto_test(test)
        self.gtr.addSuccess(test)
        self.gtr._reportOutcome.assert_called_with(test, ".",
                                                   self.gtr.colors.passing)

    def test_addSuccess_with_test_time(self):
        """
        addSuccess() sets test time to correct value
        """
        test = MagicMock()
        test.shortDescription.return_value = "a"
        test.__str__.return_value = "b"
        test = proto_test(test)
        self.gtr.addSuccess(test, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_addError(self):
        """
        addError() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addError(test, err)
        self.gtr._reportOutcome.assert_called_with(test, "E",
                                                   self.gtr.colors.error, err)

    def test_addError_with_test_time(self):
        """
        addError() sets test time to correct value
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addError(test, err, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_addFailure(self):
        """
        addFailure() makes the correct calls to other functions.
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(test, "F",
                                                   self.gtr.colors.failing,
                                                   err)

    def test_addFailure_with_test_time(self):
        """
        addFailure() makes test time the correct value
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addFailure(test, err, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_addFailureTwistedSkip(self):
        """
        Twisted's practice of calling addFailure() with their skips is detected
        and redirected to addSkip()
        """
        err = None
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        reason = "Twisted is odd"
        err = proto_error(err)
        err.traceback_lines = [
            "UnsupportedTrialFeature: ('skip', '{}')".format(reason)
        ]
        self.gtr.addFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(test,
                                                   "s",
                                                   self.gtr.colors.skipped,
                                                   reason=reason)

    def test_addSkip(self):
        """
        addSkip() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        reason = "skip reason"
        self.gtr.addSkip(test, reason)
        self.gtr._reportOutcome.assert_called_with(test,
                                                   "s",
                                                   self.gtr.colors.skipped,
                                                   reason=reason)

    def test_addSkip_with_test_time(self):
        """
        addSkip() makes test time the correct value
        """
        test = proto_test(MagicMock())
        reason = "skip reason"
        self.gtr.addSkip(test, reason, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_addExpectedFailure(self):
        """
        addExpectedFailure() makes the correct calls to other functions.
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addExpectedFailure(test, err)
        self.gtr._reportOutcome.assert_called_with(
            test, "x", self.gtr.colors.expectedFailure, err)

    def test_addExcepectedFailure_with_test_time(self):
        """
        addExpectedFailure() makes test time correct value
        """
        try:
            raise Exception
        except:
            err = sys.exc_info()
        test = proto_test(MagicMock())
        err = proto_error(err)
        self.gtr.addExpectedFailure(test, err, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_addUnexpectedSuccess(self):
        """
        addUnexpectedSuccess() makes the correct calls to other functions.
        """
        test = proto_test(MagicMock())
        self.gtr.addUnexpectedSuccess(test)
        self.gtr._reportOutcome.assert_called_with(
            test, "u", self.gtr.colors.unexpectedSuccess)

    def test_addUnexpectedSuccess_with_test_time(self):
        """
        addUnexpectedSuccess() makes test time with correct value
        """
        test = proto_test(MagicMock())
        self.gtr.addUnexpectedSuccess(test, "0.42")

        self.assertEqual(test.test_time, "0.42")

    def test_wasSuccessful(self):
        """
        wasSuccessful returns what we expect.
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        self.assertEqual(gtr.wasSuccessful(), False)
        gtr.passing.append("anything")
        self.assertEqual(gtr.wasSuccessful(), True)
        gtr.all_errors.append("anything")
        self.assertEqual(gtr.wasSuccessful(), False)

    def test_wasSuccessful_expectedFailures(self):
        """
        wasSuccessful returns what we expect when we only have expectedFailures
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.expectedFailures.append("anything")
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_passing(self):
        """
        wasSuccessful returns what we expect when we only have passing tests
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.passing.append("anything")
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_skipped(self):
        """
        wasSuccessful returns what we expect when we only have skipped tests
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.skipped.append("anything")
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_unexpectedSuccesses(self):
        """
        wasSuccessful returns what we expect when we only have unexpectedSuccesses
        """
        self.args.verbose = 1
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.unexpectedSuccesses.append("anything")
        self.assertEqual(gtr.wasSuccessful(), True)

    def test_wasSuccessful_coverageFails(self):
        """
        wasSuccessful fails if minimum coverage is not met
        """
        self.args.minimum_coverage = 50
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.coverage_percent = 49
        self.assertEqual(gtr.wasSuccessful(), False)

    def test_wasSuccessful_coverageSucceeds(self):
        """
        wasSuccessful succeds if minimum coverage is met
        """
        self.args.minimum_coverage = 50
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.passing.append("anything")
        gtr.coverage_percent = 60
        self.assertEqual(gtr.wasSuccessful(), True)