当前位置: 首页>>代码示例>>Python>>正文


Python asynctest.TestCase方法代码示例

本文整理汇总了Python中asynctest.TestCase方法的典型用法代码示例。如果您正苦于以下问题:Python asynctest.TestCase方法的具体用法?Python asynctest.TestCase怎么用?Python asynctest.TestCase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在asynctest的用法示例。


在下文中一共展示了asynctest.TestCase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_update_default_loop_works

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_update_default_loop_works(self):
        a_loop = asyncio.new_event_loop()
        self.addCleanup(a_loop.close)

        class Update_Default_Loop_TestCase(asynctest.TestCase):
            @asynctest.fail_on(unused_loop=False)
            def runTest(self):
                self.assertIs(self.loop, asyncio.get_event_loop())
                asyncio.set_event_loop(a_loop)
                self.assertIs(a_loop, asyncio.get_event_loop())

        for method in self.run_methods:
            with self.subTest(method=method):
                case = Update_Default_Loop_TestCase()
                result = getattr(case, method)()

                if result:
                    self.assertTrue(result.wasSuccessful())

                self.assertIs(a_loop, asyncio.get_event_loop()) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:22,代码来源:test_case.py

示例2: test_use_default_loop

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_use_default_loop(self):
        default_loop = self.create_default_loop()

        class Using_Default_Loop_TestCase(asynctest.TestCase):
            use_default_loop = True

            @asynctest.fail_on(unused_loop=False)
            def runTest(self):
                self.assertIs(default_loop, self.loop)

        for method in self.run_methods:
            with self.subTest(method=method):
                case = Using_Default_Loop_TestCase()
                result = getattr(case, method)()

                # assert that the original loop is reset after the test
                self.assertIs(default_loop, asyncio.get_event_loop())

                if result:
                    self.assertTrue(result.wasSuccessful())

            self.assertFalse(default_loop.is_closed()) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:24,代码来源:test_case.py

示例3: test_forbid_get_event_loop

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_forbid_get_event_loop(self):
        default_loop = self.create_default_loop()

        # Changed in python 3.6: get_event_loop() returns the running loop in
        # a callback or a coroutine, so forbid_get_event_loop should only be
        # forbidden where the loop is not running.
        @asynctest.lenient
        class Forbid_get_event_loop_TestCase(asynctest.TestCase):
            forbid_get_event_loop = True

            def runTest(self):
                with self.assertRaises(AssertionError):
                    asyncio.get_event_loop()

        for method in self.run_methods:
            with self.subTest(method=method):
                case = Forbid_get_event_loop_TestCase()
                result = getattr(case, method)()

                # assert that the original loop is reset after the test
                self.assertIs(default_loop, asyncio.get_event_loop())

                if result:
                    self.assertTrue(result.wasSuccessful()) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:26,代码来源:test_case.py

示例4: test_coroutinefunction_executed

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_coroutinefunction_executed(self):
        class CoroutineFunctionTest(asynctest.TestCase):
            ran = False

            async def noop(self):
                pass

            @asyncio.coroutine
            def runTest(self):
                self.ran = True
                yield from self.noop()

        class NativeCoroutineFunctionTest(CoroutineFunctionTest):
            async def runTest(self):
                self.ran = True
                await self.noop()

        for method in self.run_methods:
            with self.subTest(method=method):
                for case in (CoroutineFunctionTest(),
                             NativeCoroutineFunctionTest()):
                    with self.subTest(case=case):
                        case.ran = False
                        getattr(case, method)()
                        self.assertTrue(case.ran) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:27,代码来源:test_case.py

示例5: test_loop_uses_TestSelector

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_loop_uses_TestSelector(self):
        @asynctest.fail_on(unused_loop=False)
        class CheckLoopTest(asynctest.TestCase):
            def runTest(self):
                # TestSelector is used
                self.assertIsInstance(self.loop._selector,
                                      asynctest.selector.TestSelector)

                # And wraps the original selector
                self.assertIsNotNone(self.loop._selector._selector)

        for method in self.run_methods:
            with self.subTest(method=method):
                case = CheckLoopTest()
                outcome = getattr(case, method)()

                if outcome:
                    self.assertTrue(outcome.wasSuccessful()) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:20,代码来源:test_case.py

示例6: test_decorate_subclass_inherits_parent_params

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_decorate_subclass_inherits_parent_params(self):
        @asynctest.fail_on(foo=True)
        class TestCase(asynctest.TestCase):
            pass

        @asynctest.fail_on(bar=False)
        class SubTestCase(TestCase):
            pass

        @asynctest.fail_on(foo=False, bar=False)
        class OverridingTestCase(TestCase):
            pass

        self.assert_checks_equal(TestCase(), foo=True, bar=True)
        self.assert_checks_equal(SubTestCase(), foo=True, bar=False)
        self.assert_checks_equal(OverridingTestCase(), foo=False, bar=False) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_case.py

示例7: test_check_after_tearDown

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_check_after_tearDown(self):
        self.mocks['default'].side_effect = AssertionError

        class Dummy_TestCase(asynctest.TestCase):
            def tearDown(self):
                self.tearDown_called = True

            def runTest(self):
                pass

        with self.subTest(method="debug"):
            case = Dummy_TestCase()
            with self.assertRaises(AssertionError):
                case.debug()

            self.assertTrue(case.tearDown_called)

        case = Dummy_TestCase()
        result = case.run()
        self.assertEqual(1, len(result.failures))
        self.assertTrue(case.tearDown_called) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:23,代码来源:test_case.py

示例8: test_non_existing_before_test_wont_fail

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_non_existing_before_test_wont_fail(self):
        # set something not callable for default, nothing for optional, the
        # test must not fail
        setattr(asynctest._fail_on._fail_on, "before_test_default", None)
        self.addCleanup(delattr, asynctest._fail_on._fail_on,
                        "before_test_default")

        @asynctest.fail_on(default=True, optional=True)
        class TestCase(asynctest.TestCase):
            def runTest(self):
                pass

        for method in self.run_methods:
            with self.subTest(method=method):
                getattr(TestCase(), method)()
                self.assert_checked("default", "optional") 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_case.py

示例9: test_fails_when_loop_didnt_run_using_default_loop

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_fails_when_loop_didnt_run_using_default_loop(self):
        class TestCase(self.WithCheckTestCase):
            use_default_loop = True

        default_loop = self.create_default_loop()

        with self.assertRaisesRegex(AssertionError,
                                    'Loop did not run during the test'):
            TestCase().debug()

        result = TestCase().run()
        self.assertEqual(1, len(result.failures))

        default_loop.run_until_complete(asyncio.sleep(0, loop=default_loop))

        with self.assertRaisesRegex(AssertionError,
                                    'Loop did not run during the test'):
            TestCase().debug()

        default_loop.run_until_complete(asyncio.sleep(0, loop=default_loop))

        result = TestCase().run()
        self.assertEqual(1, len(result.failures)) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:25,代码来源:test_case.py

示例10: test_passes_when_ignore_loop_or_loop_run

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_passes_when_ignore_loop_or_loop_run(self):
        @asynctest.fail_on(unused_loop=False)
        class IgnoreLoopClassTest(Test.FooTestCase):
            pass

        @asynctest.fail_on(unused_loop=True)
        class WithCoroutineTest(asynctest.TestCase):
            @asyncio.coroutine
            def runTest(self):
                yield from []

        @asynctest.fail_on(unused_loop=True)
        class WithFunctionCallingLoopTest(asynctest.TestCase):
            def runTest(self):
                fut = asyncio.Future()
                self.loop.call_soon(fut.set_result, None)
                self.loop.run_until_complete(fut)

        for test in (IgnoreLoopClassTest, WithCoroutineTest,
                     WithFunctionCallingLoopTest):
            with self.subTest(test=test):
                test().debug()

                result = test().run()
                self.assertEqual(0, len(result.failures)) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:27,代码来源:test_case.py

示例11: test_fails_when_loop_ran_only_during_setup

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_fails_when_loop_ran_only_during_setup(self):
        for test_use_default_loop in (False, True):
            with self.subTest(use_default_loop=test_use_default_loop):
                if test_use_default_loop:
                    self.create_default_loop()

                class TestCase(self.WithCheckTestCase):
                    use_default_loop = test_use_default_loop

                    def setUp(self):
                        self.loop.run_until_complete(
                            asyncio.sleep(0, loop=self.loop))

                with self.assertRaisesRegex(
                        AssertionError, 'Loop did not run during the test'):
                    TestCase().debug()

                result = TestCase().run()
                self.assertEqual(1, len(result.failures)) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:21,代码来源:test_case.py

示例12: test_fails_when_loop_ran_only_during_cleanup

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_fails_when_loop_ran_only_during_cleanup(self):
        for test_use_default_loop in (False, True):
            with self.subTest(use_default_loop=test_use_default_loop):
                if test_use_default_loop:
                    self.create_default_loop()

                class TestCase(self.WithCheckTestCase):
                    use_default_loop = test_use_default_loop

                    def setUp(self):
                        self.addCleanup(asyncio.coroutine(lambda: None))

                with self.assertRaisesRegex(
                        AssertionError, 'Loop did not run during the test'):
                    TestCase().debug()

                result = TestCase().run()
                self.assertEqual(1, len(result.failures)) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:20,代码来源:test_case.py

示例13: test_events_watched_outside_test_are_ignored

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_events_watched_outside_test_are_ignored(self):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        try:
            mock = asynctest.selector.FileMock()
            loop.add_reader(mock, lambda: None)
            self.addCleanup(loop.remove_reader, mock)

            class TestCase(asynctest.TestCase):
                use_default_loop = False

                def runTest(self):
                    pass

            TestCase().debug()
        finally:
            loop.close()
            asyncio.set_event_loop(None) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:21,代码来源:test_selector.py

示例14: test_fail_on_original_selector_callback

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_fail_on_original_selector_callback(self):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        try:
            with unittest.mock.patch.object(loop, "_selector") as mock:
                class TestCase(asynctest.TestCase):
                    use_default_loop = True

                    def runTest(self):
                        # add a dummy event
                        handle = asyncio.Handle(lambda: None, (), self.loop)
                        key = selectors.SelectorKey(1, 1, selectors.EVENT_READ,
                                                    (handle, None))
                        mock.get_map.return_value = {1: key}

                with self.assertRaisesRegex(AssertionError,
                                            "some events watched during the "
                                            "tests were not removed"):
                    TestCase().debug()
        finally:
            loop.close()
            asyncio.set_event_loop(None) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:25,代码来源:test_selector.py

示例15: test_fails_when_future_has_scheduled_calls

# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import TestCase [as 别名]
def test_fails_when_future_has_scheduled_calls(self):
        class CruftyTest(asynctest.TestCase):
            @asynctest.fail_on(active_handles=True, unused_loop=False)
            def runTest(instance):
                instance.loop.call_later(5, lambda: None)

        with self.subTest(method='debug'):
            with self.assertRaisesRegex(AssertionError, 'unfinished work'):
                CruftyTest().debug()
        with self.subTest(method='run'):
            result = CruftyTest().run()
            self.assertEqual(1, len(result.failures)) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:14,代码来源:test_case.py


注:本文中的asynctest.TestCase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。