Beispiel #1
0
class TestInjectorInterceptorsTest(unittest.TestCase):
    def setUp(self):
        def test(): pass

        self.test_definition = TestDefinition(test, "unittest", "unittest", "unittest", {})
        self.injector = TestInjector()

    def test_should_execute_before_interceptor(self):
        def before():
            before.executed = True

        before.executed = False
        self.test_definition.before_interceptors.append(before)

        self.injector.execute_test(self.test_definition)

        assert_that(before.executed).is_true()

    def test_should_after_before_interceptor(self):
        def after():
            after.executed = True

        after.executed = False
        self.test_definition.after_interceptors.append(after)

        self.injector.execute_test(self.test_definition)

        assert_that(after.executed).is_true()

    def test_should_mark_test_as_failed_when_before_interceptor_raises_exception(self):
        def before():
            raise Exception("Caboom")

        self.test_definition.before_interceptors.append(before)

        results = self.injector.execute_test(self.test_definition)

        assert_that(len(results)).is_equal_to(1)
        assert_that(results[0].success).is_false()
        assert_that(results[0].message).is_equal_to("Execution of before interceptor failed: Exception: Caboom")

    def test_should_mark_test_as_failed_when_after_interceptor_raises_exception(self):
        def after():
            raise Exception("Caboom")

        self.test_definition.after_interceptors.append(after)

        results = self.injector.execute_test(self.test_definition)

        assert_that(len(results)).is_equal_to(1)
        assert_that(results[0].success).is_false()
        assert_that(results[0].message).is_equal_to("Execution of after interceptor failed: Exception: Caboom")
Beispiel #2
0
    def setUp(self):
        def test(): pass

        self.test_definition = TestDefinition(test, "unittest", "unittest", "unittest", {})
        self.injector = TestInjector()
Beispiel #3
0
 def setUp(self):
     self.injector = TestInjector()
Beispiel #4
0
class TestInjectorTest(unittest.TestCase):
    def setUp(self):
        self.injector = TestInjector()

    def test_ensure_that_test_is_marked_as_successful_when_being_executed_without_exception(self):
        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {})

        actual = self.injector.execute_test(test_definition)

        assert_that(actual[0].success).is_true()
        assert_that(actual[0].parameter_description).equals("")

    def test_ensure_that_test_is_marked_as_failing_when_being_executed_with_an_assertion_error(self):
        function = InvocationCountingFunctionMock(AssertionError("Caboom"))
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {})

        actual = self.injector.execute_test(test_definition)

        assert_that(actual[0].success).is_false()
        assert_that(actual[0].message).equals("Caboom")

    def test_ensure_that_test_is_marked_as_failing_and_exception_type_in_message_when_some_exception_raised(self):
        function = InvocationCountingFunctionMock(Exception("Caboom"))
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {})

        actual = self.injector.execute_test(test_definition)

        assert_that(actual[0].success).is_false()
        assert_that(actual[0].message).equals("Exception: Caboom")

    def test_should_invoke_test_function_once_when_no_givens_are_set(self):
        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {})

        self.injector.execute_test(test_definition)

        assert_that(function.invocation_counter).equals(1)
        assert_that(function.invocation_arguments[0]).equals({})

    def test_should_invoke_test_function_once_when_constant_given_is_set(self):
        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {"spam": "eggs"})

        result = self.injector.execute_test(test_definition)

        assert_that(function.invocation_counter).equals(1)
        assert_that(function.invocation_arguments[0]).equals({"spam": "eggs"})
        assert_that(result[0].parameter_description).equals("spam=eggs")

    def test_should_invoke_test_function_once_when_fixture_is_given_and_provide_and_reclaim_are_called(self):
        class TestFixture(Fixture):
            provide_invoked = False
            reclaim_invoked = False

            def provide(self):
                TestFixture.provide_invoked = True
                return ["spam"]

            def reclaim(self, value):
                TestFixture.reclaim_invoked = True
                assert_that(value).equals("spam")

        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {"spam": TestFixture})

        self.injector.execute_test(test_definition)

        assert_that(function.invocation_counter).equals(1)
        assert_that(function.invocation_arguments[0]).equals({"spam": "spam"})

        assert_that(TestFixture.provide_invoked).is_true()
        assert_that(TestFixture.reclaim_invoked).is_true()

    def test_should_invoke_test_function_twice_when_fixture_provides_two_values(self):
        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module",
                {"spam": enumerate("spam", "eggs")})

        self.injector.execute_test(test_definition)

        assert_that(function.invocation_counter).equals(2)

    def test_should_reclaim_all_values_when_fixture_returns_more_than_one_value(self):
        class TestFixture(Fixture):
            provide_invoked = 0
            reclaim_invoked = 0

            def provide(self):
                TestFixture.provide_invoked += 1
                return ["spam", "eggs"]

            def reclaim(self, value):
                TestFixture.reclaim_invoked += 1

        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module", {"spam": TestFixture})

        self.injector.execute_test(test_definition)

        assert_that(TestFixture.provide_invoked).equals(1)
        assert_that(TestFixture.reclaim_invoked).equals(2)

    def test_should_invoke_test_function_four_times_when_two_fixtures_each_provide_two_values(self):
        function = InvocationCountingFunctionMock()
        test_definition = TestDefinition(function, "unittest", "unittest", "module",
                {"spam": enumerate("spam", "eggs"),
                 "foo": enumerate("foo", "bar")})

        self.injector.execute_test(test_definition)

        assert_that(function.invocation_counter).equals(4)
        assert_that(function.invocation_arguments).contains({"spam": "spam", "foo": "foo"})
        assert_that(function.invocation_arguments).contains({"spam": "spam", "foo": "bar"})
        assert_that(function.invocation_arguments).contains({"spam": "eggs", "foo": "foo"})
        assert_that(function.invocation_arguments).contains({"spam": "eggs", "foo": "bar"})

    def test_should_collect_traceback_when_test_function_raises_an_exception(self):
        def test_function():
            raise Exception("Caboom")

        test_definition = TestDefinition(test_function, "unittest", "unittest", "module", {})

        results = self.injector.execute_test(test_definition)

        assert_that(len(results)).is_equal_to(1)
        assert_that(results[0].success).is_false()
        assert_that(results[0].traceback).is_not_none()
        assert_that(results[0].traceback_as_string).matches(".*")