Proper way to test existing routes
See original GitHub issueunittest.TestCase
I have read the docs. However, I couldn’t quite figure out how to write test cases with the unittest.TestCase
class.
I am trying to use the autofixture feature here: https://docs.pytest.org/en/latest/unittest.html and also to try to organize my code better.
Proper route testing
I am slightly confused by the following from the docs http://pytest-sanic.readthedocs.io/en/latest/fixtures.html
@pytest.yield_fixture
def app():
app = Sanic("test_sanic_app")
@app.route("/test_get", methods=['GET'])
async def test_get(request):
return response.json({"GET": True})
If I have an existing sanic app that requires testing, why am I writing routes here? Are these test routes or the app’s routes?
Using your documentation, I have been writing the test pages like this but am unsure if this is the proper way to do it.
from cr.app import app as webapp
@pytest.yield_fixture
def app():
_webapp = webapp
yield _webapp
@pytest.fixture
def test_cli(loop, app, test_client):
return loop.run_until_complete(test_client(app))
async def test_index(test_cli):
resp = await test_cli.get('/')
assert resp.status == 200
async def test_player(test_cli):
resp = await test_cli.get('/player')
assert resp.status == 200
Any help would be appreciated.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
How to test Apache Camel Routes using Camel Test Kit ...
How to test Apache Camel Routes using Camel Test Kit? Camel Unit Testing, Mock, AdviceWith, Test KitTesting is a crucial activity in any ......
Read more >Testing Routes With Postman - Medium
To make sure the routes are set up correctly, you should test them. If you have a small app with just a few...
Read more >Integration testing on existing routes with Apache Camel (and ...
This article is here to show how to do integration tests with Apache Camel in a Spring App, since it is not described...
Read more >The Guide to Testing in Apache Camel - Tom Donohue
Learn how to test your Camel routes, standalone or in Spring Boot. ... to return MyRouteBuilder , which contains the routes to be...
Read more >Chapter 8. To Test a Route with JUnit Red Hat JBoss Fuse 6.2
Select all trace-generated messages in batch, right-click to open the context menu, and select Delete. Deleting the existing test case. To delete the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The example in this issue should replace the current quickstart example! It’s much more realistic & useful than the standalone one. I was confused for hours, not sure why the test code was creating routes, and couldn’t figure out how the real routes were getting into the test. (It shouldn’t and they weren’t; that’s a toy example that mixes test code with real code.) This is what is working for me:
because it starts
Sanic
in async way, https://github.com/channelcat/sanic/blob/master/sanic/app.py#L719.As for unit tests, we need a
loop
to run all async unit tests under the hood.(async def test_
)Normally, when you write your application, if you don’t explicitly pass
loop
, that means you just use the defaultloop
fromasyncio.get_event_loop()
. it doesn’t mean you don’t need event loop.Furthermore, you can simply instantiate the sanic app with
app = Sanic(__name__)
, we just hide the loop part:https://github.com/channelcat/sanic/blob/master/sanic/app.py#L743 https://github.com/channelcat/sanic/blob/master/sanic/server.py#L563 https://github.com/channelcat/sanic/blob/master/sanic/worker.py#L45