Verified Commit fd70ba6c authored by sebian's avatar sebian
Browse files

blog: 2016 - Move articles in a separated directory, and remove test article

parent f80871a7
Title: Pytest Fixture
Category: tuto
Tags: python, dev
I am a huge fan of python (one of the best language in my toolbox). And
when it comes to test, [pytest]( is *THE* library to use.
I also use [Flask]( a lot, so today I will show you
some of my snippets.
This creates an app fixture which will be used for testing the application, it
returns a test client to interact with my Flask application.
It is an adaptation of the documentation [testing skeleton]
I also replace the json encoder by a custom one (it allows me to dump mock
object for example).
def request_context(app):
"""Provide a Flask request context for testing purpose"""
with app.application.test_request_context() as req_context:
yield req_context
This one applies a request context on a testing function, this can be
useful if you manipulate werzeug interactions (flask request attribute mostly).
This has to be used when the
`RuntimeError: working outside of application context` error is raised.
In the most common use case you will not need the `req_context` variable during
your test. To avoid having an unused argument, you can simply use the
`@pytest.mark.usefixtures('request_context')` decorator on your testing
Last one is also an opportunity to talk about
[peewee]( which is a great lightweight
ORM. Like a lot of ORM peewee provides a [transaction decorator]
, in unittest you may want to test endpoints without connecting to a db.
So, here is an example on how you can replace the decorator
(or contextmanager) by a dummy one.
import imp
import contextlib
import pytest
import database
@pytest.fixture(autouse=True, scope='session')
def mock_transaction():
"""Replace the atomic decorator from peewee to a noop one"""
database.atomic = contextlib.contextmanager(lambda: (yield))
Pytest uses a lot of the python flexibility and I must confess that I love it.
Title: Yet another tutorial about Let's Encrypt
Author: Sebastien Badia
Category: Tech
Tags: https, letsencrypt
Date: 2016-03-07 20:00
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment