question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way to test existing routes

See original GitHub issue

unittest.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:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
garyocommented, Jun 1, 2018

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:

# define create_app() in your server.py
sys.path.append(str(pytest.config.rootdir)) # your app's toplevel dir
from app.server import create_app

@pytest.yield_fixture
def testapp():
    app = create_app()
    yield app

# ... test as usual
0reactions
yunstanfordcommented, Feb 17, 2018

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 default loop from asyncio.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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found