Python asynctest.TestCase() Examples
The following are 24
code examples of asynctest.TestCase().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
asynctest
, or try the search function
.
Example #1
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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())
Example #2
Source File: test_selector.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_selector.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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))
Example #5
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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))
Example #6
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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))
Example #7
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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))
Example #8
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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")
Example #9
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #10
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #11
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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())
Example #12
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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)
Example #13
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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())
Example #14
Source File: test_case.py From asynctest with Apache License 2.0 | 6 votes |
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())
Example #15
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def test_decorate_subclass_doesnt_affect_base_class(self): class TestCase(asynctest.TestCase): pass @asynctest.fail_on(foo=True) class SubTestCase(TestCase): pass self.assert_not_decorated(TestCase()) self.assert_checks(SubTestCase(), foo=True)
Example #16
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def test_decorate_method(self): class TestCase(asynctest.TestCase): @asynctest.fail_on(foo=True) def test_foo(self): pass instance = TestCase() self.assert_checks_equal(instance.test_foo, foo=True, bar=True)
Example #17
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def test_strict_decorator(self): @asynctest.strict class TestCase(asynctest.TestCase): pass self.assert_checks_equal(TestCase(), foo=True, bar=True) @asynctest.strict() class TestCase(asynctest.TestCase): pass self.assert_checks_equal(TestCase(), foo=True, bar=True)
Example #18
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def test_lenient_decorator(self): @asynctest.lenient class TestCase(asynctest.TestCase): pass self.assert_checks_equal(TestCase(), foo=False, bar=False) @asynctest.lenient() class TestCase(asynctest.TestCase): pass self.assert_checks_equal(TestCase(), foo=False, bar=False)
Example #19
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def checks(self, obj, fatal=True): if isinstance(obj, asynctest.TestCase): case = obj else: case = obj.__self__ try: return getattr(obj, asynctest._fail_on._FAIL_ON_ATTR).get_checks(case) except AttributeError: if fatal: self.fail("{!r} not decorated".format(obj))
Example #20
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
def test_cleanup_functions_can_be_coroutines(self): cleanup_normal_called = False cleanup_normal_called_too_soon = False cleanup_coro_called = False def cleanup_normal(): nonlocal cleanup_normal_called cleanup_normal_called = True @asyncio.coroutine def cleanup_coro(): nonlocal cleanup_coro_called cleanup_coro_called = True @asynctest.fail_on(unused_loop=False) class TestCase(Test.FooTestCase): def setUp(self): nonlocal cleanup_normal_called, cleanup_normal_called_too_soon nonlocal cleanup_coro_called cleanup_normal_called = cleanup_coro_called = False self.addCleanup(cleanup_normal) cleanup_normal_called_too_soon = cleanup_normal_called self.addCleanup(cleanup_coro) for method in self.run_methods: with self.subTest(method=method): getattr(TestCase(), method)() self.assertTrue(cleanup_normal_called) self.assertTrue(cleanup_coro_called)
Example #21
Source File: test_case.py From asynctest with Apache License 2.0 | 5 votes |
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))
Example #22
Source File: test_selector.py From asynctest with Apache License 2.0 | 5 votes |
def test_passes_when_no_callbacks_left(self): class TestCase(asynctest.TestCase): def runTest(self): mock = asynctest.selector.FileMock() self.loop.add_reader(mock, lambda: None) self.loop.remove_reader(mock) TestCase().debug()
Example #23
Source File: test_selector.py From asynctest with Apache License 2.0 | 5 votes |
def test_fail_on_active_selector_callbacks_on_mock_files(self): class TestCase(asynctest.TestCase): def runTest(self): mock = asynctest.selector.FileMock() self.loop.add_reader(mock, lambda: None) # it's too late to check that during cleanup self.addCleanup(self.loop.remove_reader, mock) with self.assertRaisesRegex(AssertionError, "some events watched " "during the tests were not removed"): TestCase().debug()
Example #24
Source File: test_case.py From asynctest with Apache License 2.0 | 4 votes |
def test_init_and_close_loop_for_test(self): default_loop = self.create_default_loop() @asynctest.lenient class LoopTest(asynctest.TestCase): failing = False def runTest(self): try: self.assertIsNotNone(self.loop) self.assertFalse(self.loop.close.called) except Exception: self.failing = True raise def runFailingTest(self): self.runTest() raise SystemError() for method, test in itertools.product(self.run_methods, ('runTest', 'runFailingTest', )): with self.subTest(method=method, test=test), \ unittest.mock.patch('asyncio.new_event_loop') as mock: mock_loop = unittest.mock.Mock(asyncio.AbstractEventLoop) mock_loop.run_until_complete.side_effect = default_loop.run_until_complete if sys.version_info >= (3, 6): mock_loop.shutdown_asyncgens = asynctest.CoroutineMock() mock.return_value = mock_loop case = LoopTest(test) try: getattr(case, method)() except SystemError: pass mock.assert_called_with() mock_loop.close.assert_called_with() if sys.version_info >= (3, 6): mock_loop.shutdown_asyncgens.assert_awaited() # If failing is True, one of the assertions in runTest failed self.assertFalse(case.failing) # Also, ensure we didn't override the original loop self.assertIs(default_loop, asyncio.get_event_loop())