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

Introduce fixture for test client class redefinition #4706

Merged

Conversation

hoefling
Copy link
Contributor

@hoefling hoefling commented Apr 22, 2020

Signed-off-by: oleg.hoefling oleg.hoefling@gmail.com

What do these changes do?

A new fixture named aiohttp_client_cls is added that serves as TestClient factory for the aiohttp_client fixture.

When writing tests against aiohttp apps, we often need to extend the TestClient with methods that hide the low-level boilerplate, e.g.

class GraphqlClient(TestClient):
    async def query(self, **kwargs):
        response = await self.post(
            '/graphql', json=kwargs, headers=dict(content_type='application/json')
        )
        return response.json()

To replace the TestClient with the custom client impl, we have to copy the aiohttp_client fixture out as there's no safe way to inject the GraphqlClient class.

The changes in this PR offer a possibility to pass a custom test client impl. Example:

# conftest.py

@pytest.fixture
def aiohttp_client_cls():
    return GraphqlClient

# test_module.py

def test_query(aiohttp_client):
    app: Application = app_factory()
    client = await aiohttp_client(app)
    payload = await client.query(
        query='query Greet($who: String!) {greet($who) { message }}',
        variables={'who': 'world'}
    )
    assert 'errors' not in payload
    assert payload['data']['message'] == 'hello world'

A more common example could be providing a login()/logout() method pair, similar to Django/Flask's test clients.

Are there changes in behavior for the user?

Nothing will change for existing test suites.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes - added an example in fixture docstring and a reference in sphinx docs
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES folder

Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Apr 22, 2020
@webknjaz
Copy link
Member

I like it, go ahead and add tests.

Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
@hoefling
Copy link
Contributor Author

@webknjaz done - the default case is already covered by the existing suite, added tests for custom client class passed. Also extended docs with another example on how to pass test specific client impls, should anyone need it.

@webknjaz webknjaz closed this May 1, 2020
@webknjaz webknjaz reopened this May 1, 2020
@webknjaz webknjaz changed the title introduce fixture for test client class redefinition Introduce fixture for test client class redefinition May 1, 2020
@webknjaz webknjaz merged commit cd5c48a into aio-libs:master May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants