Skip to content

Commit

Permalink
Merge 7f39469 into 8f0a5ea
Browse files Browse the repository at this point in the history
  • Loading branch information
przerull committed Aug 2, 2016
2 parents 8f0a5ea + 7f39469 commit ac80c95
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
30 changes: 22 additions & 8 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,28 @@ def get_app(self, loop):
"""
pass

def setUp(self):
self.loop = setup_test_loop()
self.app = self.get_app(self.loop)
self.client = TestClient(self.app)

def tearDown(self):
del self.client
teardown_test_loop(self.loop)
@classmethod
def setUpClass(cls):
super(AioHTTPTestCase, cls).setUpClass()

def setup_decorator(method):
def decorated(self, *args, **kwargs):
self.loop = setup_test_loop()
self.app = self.get_app(self.loop)
self.client = TestClient(self.app)
return method(self, *args, **kwargs)
return decorated

def teardown_decorator(method):
def decorated(self, *args, **kwargs):
result = method(self, *args, **kwargs)
del self.client
teardown_test_loop(self.loop)
return result
return decorated

cls.setUp = setup_decorator(cls.setUp)
cls.tearDown = teardown_decorator(cls.tearDown)


def unittest_run_loop(func):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ def test_test_client_close_is_idempotent():
client.close()


def test_setup_and_teardown_for_aiohttp_test_case():
class TestSetupAndTearDown(AioHTTPTestCase):
setup_run = False
teardown_run = False

def get_app(self, loop):
return _create_example_app(loop)

def setUp(self):
self.setup_run = True

def tearDown(self):
self.teardown_run = True
TestSetupAndTearDown.setUpClass()
testcase = TestSetupAndTearDown()
assert not testcase.setup_run
assert not testcase.teardown_run
assert not hasattr(testcase, 'client')
testcase.setUp()
print(dir(testcase))
assert hasattr(testcase, 'client')
assert testcase.setup_run
assert not testcase.teardown_run
testcase.tearDown()
assert not hasattr(testcase, 'client')
assert testcase.setup_run
assert testcase.teardown_run


class TestAioHTTPTestCase(AioHTTPTestCase):

def get_app(self, loop):
Expand Down

0 comments on commit ac80c95

Please sign in to comment.