aws / aws/chalice

[proposal] Chalice Test Client

Open
#1,468 6 comments 10 reactions 0 assignees View on GitHub
proposals
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

# Test Client Proposal

This proposal outlines improvements to Chalice that enable
users to easily test their applications.

## Goals

* Add new Chalice APIs for easily testing a Chalice application.
* Must support not only REST APIs, but all other Chalice event handlers.
Even if all event handlers are not implemented initially, the design
of the APIs should be finished.
* We want the API to feel familiar to python developers. While we will
need to adapt to Lambda, we don't want to stray too far from existing
testing APIs that developers use in other frameworks. This API should not
have a steep learning curve.

## Non Goals

Part of successfully testing an app requires the ability to swap out
objects with mock/fake versions. This is out of scope for this proposal, but
is addressed in a separate follow up proposal. This proposal focuses on new
testing APIs through clients that allow you to invoke your lambda handlers
and rest/websocket APIs.

## Prior Work

* Test client for Chalice rest APIs: https://github.com/aws/chalice/issues/1120
Corresponding PR: https://github.com/aws/chalice/pull/1193

* Tracking github issue for testing: https://github.com/aws/chalice/issues/289
"Sample for writing tests for Chalice app"

The proposal mostly builds off that work and generalizes it to include
testing for all of Chalice'event handlers.

### Flask

Docs for testing flask apps: https://flask.palletsprojects.com/en/1.1.x/testing/

Flask lets you test through a context manager, the basic test looks like:

```python

def test_foo():
with mymodule.app.test_client() as client:
rv = client.get('/')
assert b'my test' in rv.data

```

They also have some documentation on how to use this with pytest.
There's also a more low level context manager that lets you set
the context:

```
import flask

app = flask.Flask(__name__)

with app.test_request_context('/?name=Peter'):
assert flask.request.path == '/'
assert flask.request.args['name'] == 'Peter'
```

### Django

Django also has a [test client](https://docs.djangoproject.com/en/3.0/topics/testing/tools/):

```
>>> from django.test import Client
>>> c = Client()
>>> response = c.post('/login/', {'username': 'john', 'password': 'smith'})
>>> response.status_code
200
>>> response = c.get('/customer/details/')
>>> response.content
b'

Contributor guide

Open the contributing guide

Research direction

Start with the proposed chalice.test module and the existing LocalGateway behavior described in the proposal. Review how the Client would cover http, ws, events, and lambda_ while honoring .chalice/config.json. Done means agreeing on the design and implementing the specified testing interfaces with coverage for the proposed handler types.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
api, backend, testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.