Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a TestClient object inside a unittest_run_loop decorated AioHTTPTestCase method #2505

Closed
nastasi opened this issue Nov 10, 2017 · 9 comments
Labels

Comments

@nastasi
Copy link

nastasi commented Nov 10, 2017

Long story short

I'm currently not able to create a new TestClient object (I need it because I must set cookies) in a
function like:

class EnjoyTestCase(AioHTTPTestCase):
    @unittest_run_loop
    async def test_cookies(self):
        tc = TestClient( ....

Expected behaviour

Instead to works I retrieve an error about
self._root that has no join method (is None) at line 87 of test_utils.py file.

Idea about ? There is a proper set of arguments that fix the problem ?
If there is a way I will subscribe a PR with a proper example.

@kxepal
Copy link
Member

kxepal commented Nov 10, 2017

Please provide full example which causes an error and the exception about. So far it's hard to say what's wrong.

@nastasi
Copy link
Author

nastasi commented Nov 10, 2017

@kxepal this is a complete and self-contained example; if I'm creating TestClient in the wrong way please help me because I have not found a better way.

#!/usr/bin/env python
# run it as:
#   python -m unittest test_example.TestClientCase

from aiohttp import web
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loo
from aiohttp.test_utils import TestClient, TestServer


class TestClientCase(AioHTTPTestCase):
    def _app_index(self, request):
        return web.Response(body="<html><body>Here we are</body></html",
                            content_type='text/html')

    def _app_config(self, app):
        router = app.router
        router.add_get('/', self._app_index)

    async def get_application(self):
        app = web.Application()
        app.on_startup.append(self._app_config)

        return app

    @unittest_run_loop
    async def test_using_class_attribute(self):
        request = await self.client.request("GET", "/")
        print(request.status)
        assert request.status == 200

    @unittest_run_loop
    async def test_using_client(self):
        tc = TestClient(
            TestServer(self.app, loop=self.loop),
            loop=self.loop)
        request = await tc.request("GET", "/")
        print(request.status)
        assert request.status == 200

@asvetlov
Copy link
Member

@nastasi
Copy link
Author

nastasi commented Nov 11, 2017

Another error: adding await tc.start_server() just after tc = TestClient( ... I obtain:

ERROR: test_using_client (test_example.TestClientCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/nastasi/git/aiohttp/aiohttp/test_utils.py", line 426, in new_func
    func(self, *inner_args, **inner_kwargs))
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/home/nastasi/git/ao-enjoy/enjoy/tests/test_example.py", line 37, in test_using_client
    await tc.start_server()
  File "/home/nastasi/git/aiohttp/aiohttp/test_utils.py", line 207, in start_server
    yield from self._server.start_server(loop=self._loop)
  File "/home/nastasi/git/aiohttp/aiohttp/test_utils.py", line 74, in start_server
    handler = yield from self._make_factory(**kwargs)
  File "/home/nastasi/git/aiohttp/aiohttp/test_utils.py", line 153, in _make_factory
    yield from self.app.startup()
  File "/home/nastasi/git/aiohttp/aiohttp/web.py", line 259, in startup
    yield from self.on_startup.send(self)
  File "/home/nastasi/git/aiohttp/aiohttp/signals.py", line 51, in send
    yield from self._send(*args, **kwargs)
  File "/home/nastasi/git/aiohttp/aiohttp/signals.py", line 15, in _send
    res = receiver(*args, **kwargs)
  File "/home/nastasi/git/ao-enjoy/enjoy/tests/test_example.py", line 18, in _app_config
    router.add_get('/', self._app_index)
  File "/home/nastasi/git/aiohttp/aiohttp/web_urldispatcher.py", line 926, in add_get
    name=head_name, **kwargs)
  File "/home/nastasi/git/aiohttp/aiohttp/web_urldispatcher.py", line 882, in add_route
    resource = self.add_resource(path, name=name)
  File "/home/nastasi/git/aiohttp/aiohttp/web_urldispatcher.py", line 874, in add_resource
    self.register_resource(resource)
  File "/home/nastasi/git/aiohttp/aiohttp/web_urldispatcher.py", line 849, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

@asvetlov
Copy link
Member

This is correct: you should not add new routes in on_setup but do activities like connecting to database etc.

@nastasi
Copy link
Author

nastasi commented Nov 12, 2017

So, briefly, it is not possible to create a second TestClient object with the same functionalities of the .client in a @unittest_run_loop decorated class method, it's correct ?

@asvetlov
Copy link
Member

Sorry, I don't follow.

@nastasi
Copy link
Author

nastasi commented Nov 17, 2017

@asvetlov sorry for the waste of time, things are more complicated then I expected, I will study the codebase better and I will return later if needed. Tnx.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants