summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2022-03-14 11:54:13 (GMT)
committerGitHub <noreply@github.com>2022-03-14 11:54:13 (GMT)
commit9523c0d84f351a610dc651b234461eb015fa3b82 (patch)
tree5f6f6bed4353eb9c149f65ab2dc95db12d378db3 /Lib/unittest/test
parent2153daf0a02a598ed5df93f2f224c1ab2a2cca0d (diff)
downloadcpython-9523c0d84f351a610dc651b234461eb015fa3b82.zip
cpython-9523c0d84f351a610dc651b234461eb015fa3b82.tar.gz
cpython-9523c0d84f351a610dc651b234461eb015fa3b82.tar.bz2
bpo-46994: Accept explicit contextvars.Context in asyncio create_task() API (GH-31837)
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r--Lib/unittest/test/test_async_case.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index 3717486..7dc8a6b 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -1,4 +1,5 @@
import asyncio
+import contextvars
import unittest
from test import support
@@ -11,6 +12,9 @@ def tearDownModule():
asyncio.set_event_loop_policy(None)
+VAR = contextvars.ContextVar('VAR', default=())
+
+
class TestAsyncCase(unittest.TestCase):
maxDiff = None
@@ -24,22 +28,26 @@ class TestAsyncCase(unittest.TestCase):
def setUp(self):
self.assertEqual(events, [])
events.append('setUp')
+ VAR.set(VAR.get() + ('setUp',))
async def asyncSetUp(self):
self.assertEqual(events, ['setUp'])
events.append('asyncSetUp')
+ VAR.set(VAR.get() + ('asyncSetUp',))
self.addAsyncCleanup(self.on_cleanup1)
async def test_func(self):
self.assertEqual(events, ['setUp',
'asyncSetUp'])
events.append('test')
+ VAR.set(VAR.get() + ('test',))
self.addAsyncCleanup(self.on_cleanup2)
async def asyncTearDown(self):
self.assertEqual(events, ['setUp',
'asyncSetUp',
'test'])
+ VAR.set(VAR.get() + ('asyncTearDown',))
events.append('asyncTearDown')
def tearDown(self):
@@ -48,6 +56,7 @@ class TestAsyncCase(unittest.TestCase):
'test',
'asyncTearDown'])
events.append('tearDown')
+ VAR.set(VAR.get() + ('tearDown',))
async def on_cleanup1(self):
self.assertEqual(events, ['setUp',
@@ -57,6 +66,9 @@ class TestAsyncCase(unittest.TestCase):
'tearDown',
'cleanup2'])
events.append('cleanup1')
+ VAR.set(VAR.get() + ('cleanup1',))
+ nonlocal cvar
+ cvar = VAR.get()
async def on_cleanup2(self):
self.assertEqual(events, ['setUp',
@@ -65,8 +77,10 @@ class TestAsyncCase(unittest.TestCase):
'asyncTearDown',
'tearDown'])
events.append('cleanup2')
+ VAR.set(VAR.get() + ('cleanup2',))
events = []
+ cvar = ()
test = Test("test_func")
result = test.run()
self.assertEqual(result.errors, [])
@@ -74,13 +88,17 @@ class TestAsyncCase(unittest.TestCase):
expected = ['setUp', 'asyncSetUp', 'test',
'asyncTearDown', 'tearDown', 'cleanup2', 'cleanup1']
self.assertEqual(events, expected)
+ self.assertEqual(cvar, tuple(expected))
events = []
+ cvar = ()
test = Test("test_func")
test.debug()
self.assertEqual(events, expected)
+ self.assertEqual(cvar, tuple(expected))
test.doCleanups()
self.assertEqual(events, expected)
+ self.assertEqual(cvar, tuple(expected))
def test_exception_in_setup(self):
class Test(unittest.IsolatedAsyncioTestCase):