Browsed by
Tag: python

Testing a Flask REST API using Factory Boy and an in-memory sqlite database

Testing a Flask REST API using Factory Boy and an in-memory sqlite database

I’m developing a REST API using Python’s Flask microframework. As I wanted to do test driven development to ensure good test coverage, I needed a working testing framework. After checking out a few different options, I found one that works well for my use case. I thought I’d share my setup in case it may be of help to others. First, this is how I create the server:

Passing custom HTTP headers using Flask’s test_client

Passing custom HTTP headers using Flask’s test_client

Passing custon HTTP headers using Flask’s test_client object should have been as easy as doing this:

For some reason this didn’t work in my Flask project. In case others have the same issue as I did, maybe the solution I found to work may work for others too:

 

Mocking SQLAlchemy models in Python

Mocking SQLAlchemy models in Python

I’m developing an API using Python’s Flask framework. The API uses SQLAlchemy, which means that database rows are made available as regular Python classes (referred to as database models) in my Python code. One of my models looks something like this:

My Data Access Object (DAO) for communicating with the database models looks like this:

While writing tests for the API, I wanted to mock out the database models, so that my tests wouldn’t depend on an actual…

Read More Read More