Giter Site home page Giter Site logo

pytest-chalice's Introduction

pytest-chalice

PyPI version Python versions See Build Status on Travis CI

A set of py.test fixtures for AWS Chalice


Features

  • Launch the local gateway per test function
  • Provide an abstracted client fixture to access the local gateway
  • Expose an interface to overwrite response context with arbitrary objects
    • As of Chalice version 1.8.0, LocalGateway object doesn't handle Cognito's context
    • Not only for this purpose, it's an interface provided to allow custom contexts in unit tests

Requirements

Installation

You can install "pytest-chalice" via pip from PyPI:

$ pip install pytest-chalice

Usage

from chalice import Chalice

app = Chalice(__name__)


@app.route('/')
def index:
    return {'hello': 'world'}

pytest-chalice expects a fixture called app which will be the chalice app instance from your project. You can create it in your conftest.py:

import pytest

from chalice import Chalice

from project.app import app as chalice_app


@pytest.fixture
def app() -> Chalice:
    return chalice_app
from http import HTTPStatus


def test_index(client):
    response = client.get('/')
    assert response.status_code == HTTPStatus.OK
    assert response.json == {'hello': 'world'}

See examples for more detailed

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-chalice" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

pytest-chalice's People

Contributors

studio3104 avatar mazulo avatar

Stargazers

Vitor Silva avatar Alex Hafner avatar  avatar Gerhard Scheffler avatar Christo De Lange avatar Taylor D. Edmiston avatar David Gidwani avatar Arthur Alvim avatar Nikola Selic avatar Michael S. Manley @ PGS avatar  avatar Elliot Scribner avatar Murilo Viana avatar Carlos Cabrera avatar  avatar Midoru avatar Takuya Kodama avatar Chris Slamar avatar Shota Yamazaki avatar Fábio C. Barrionuevo da Luz avatar Paulo avatar

Watchers

Paulo avatar  avatar  avatar

pytest-chalice's Issues

app fixture is required

Hi! First of all thank you for building this! It's being really helpful.

I've found one issue that I think can be fixed with some more details on the README. If I follow strictly what we have there, write one test and run it that's what I will have:

# this is my test
def test_health_check_is_alive(client):
    expected_result = {'status': 'alive'}

    response = client.get('/health-check')
    result = response.json

    assert result == expected_result

and below is the output

file /tests/integration/misc/test_healt_check.py, line 1
  def test_health_check_is_alive(client):
file /lib/python3.8/site-packages/pytest_chalice/fixtures.py, line 12
  @pytest.fixture
  def client(app):
E       fixture 'app' not found

The client fixture really expects another fixture called app. I found in the examples that I'd just need to create a conftest.py with the app as fixture to solve it.

If you think it makes sense I can open a PR later to add it to the README.rst 😄

TODO

  • Add type hintings
  • Write README
  • Prepare Sphinx
  • Enhance tests
  • Write a tiny example
  • Write an example for overwriting the context

POST and PUT don't pass "body" to the handler

I tried to test a Chalice handler that responds to POST with a body=... but whatever I do It always comes out as None on my app side.

This is my test:

    response = client.post('/mm?abcd=efgh', 
        headers={"X-Something": "Some Thing"},
        body=json.dumps({
            "message_id": "1234-1234",
        })
    )

However in my /mm handler on the app side I always get app.current_request.json_body as None. The headers are passed over but the body isn't. Can you have a look a it please?

Would love more examples

I've created a relatively robust chalice app, and am literally just getting into my very first foray of unit testing (having never done it in python before).

I've created the same confttest and test_app file you created, but honestly have no idea where to go from there.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.