-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
User application testing facility #423
Comments
I believe all mentioned projects are exist because it's hard to write test server in classical blocking way. |
I don't think that internal complexity really matters for an end user. She has certain use cases and there are common approaches to solve them without too much of boilerplate. Your answer, in other words, is DIY, which is actually an answer. Anyway, I want to point to some things. Client testingSpecifically it is a white-box testing. A system written with Server testingThe threaded server, CherryPy, didn't convince you. Here's my next attempt: http://tornado.readthedocs.org/en/latest/testing.html. You can see the same |
Well, I see your point. For client testing fake connector that doesn't resolve host but connects to specific local port would be helpful. Also we can introduce a Currently we duplicate Personally I don't feel the strong request for those changes but I'm open for Pull Request. |
massive +1 for docs and/or helper methods for unit testing a server app. In my few days starting to use aiohttp this has been the biggest stubbing block and the closest I've come to saying "aiohttp is not ready, lets go back to django/flask". It's really important there's an easy and powerful way to make fake http requests, where one can:
I've made a start on doing this myself by copying bits of aiohttp's own tests but it's not obvious and there ends up being quite a lot of code dedicated to setting up the tests which I would expect to normally like in the test framework. (eg. most of If the expected approach at present is to use aiohttp's own tests as an example some more comments particularly in |
is it possible to use |
Saying aiohttp is not ready, lets go back to django/flask sounds like you only look in the spotlight, without really caring about practical qualities of the libraries/frameworks. Non-blocking IO doesn't come for free. It's good for one things, it's bad for others. If you're really looking for a non-blocking library/framework it's quite more reasonable to go back Tornado. It's the "now". aiohttp is more the "future". |
aiohttp tests was started from classic unittest style. Anyway, reading aiohttp test suite code is not so hard, isn't it? |
We at Center for Open Science developed a "port" of httpretty for aiohttp: https://github.com/CenterForOpenScience/aiohttpretty . It's in its early days, but it's certainly come in handy for aiohttp client testing. |
I have created a testing pattern that seems to work (based on very limited testing). Not sure if this is a very sane way to do it, but using this technique the tests and event loop are all running in the same main thread, so it should allow you to properly mock things. EDIT Removed the example so I don't send people down the wrong track... |
@dgelvin it may look surprising but you really don't need a separate thread for running client requests: they may share the same event loop. |
@asvetlov I believe using my technique the client requests are performed on the same event loop. The thread is just used to schedule the request on the event loop, block for a response, then stop the event loop. |
I feel your pain, guys. The main challenge for me is introducing the test framework, not particular classes/functions but the complete solution. Test framework means it should provide carefully calibrated tools for end users, covering not only aiohttp test needs but acceptable for application developers. We have plenty tests for I hope to introduce something next month -- perhaps for py.test first. |
@dgelvin but you really don't need a thread at all -- please take a look on https://github.com/KeepSafe/aiohttp/blob/master/tests/test_urldispatch.py tests |
@asvetlov Thanks, very helpful. I see that simply using |
I have some fixtures for pytest and aiohttp. They're based on the ones from aiohttp itself as well as pytest asyncio but are significantly simplified as they concentrate on testing the application not the framework. I might try taking them out of the private repo they're currently in and create them as a gist or PR. Obviously the test fixtures should end up as a reusable library rather than just available to be copy & pasted. If we're using pytest (which I really like) this should obviously be done as a plugin. @asvetlov in an ideal world do you think this plugin should be part of aiohttp or a separate package? |
In fact, just found I'm using them in a public repo: https://github.com/em-2/em2-core/blob/master/tests/http/conftest.py demonstrates a very simple The tests are only really a sub so far, but they demonstrate the idea: async def test_missing_conversation_valid_html(client):
headers = {'Authorization': 'Token 321', 'Actor': 'test@example.com'}
data = '{"key": "foobar"}'
r = await client.post('/123/messages/add/', data=data, headers=headers)
assert r.status == 400
content = await r.read()
assert content == b'ConversationNotFound: conversation 123 not found\n' |
proposal for #423 adding test utilities to aiohttp.
Does
aiohttp
provide functionality to test application written with its HTTP client? It is in the sense of packages like httpretty, responses and this SO question. Basically it may be some mock connector to say here's the next response your should return on any request. Or it may be more elaborate with separate queues for HTTP methods, base URLs, et cetera. It's useful for integration and system testing. Workarounds are possible, but require to construct complete server to imitate the target and to patch the application's URLs, which can take some substantial effort.Even though it's not in my direct interest, the same thing may also apply to HTTP server. Some sort of base test case class that provides means to start user's app, do requests and make assertions about them with ease. Here is how CherryPy does it.
I've looked at the test suite and
aiohttp.test_utils
but there seems to be nothing what I'm looking for exactly. If I have not overlooked it and there's no such functionality at the moment, this can be treated as a feature request.The text was updated successfully, but these errors were encountered: