Browsed by
Tag: python

Python: Writing test to verify command line arguments

Python: Writing test to verify command line arguments

I wrote a small Python script that will be executed from the command line. After finishing the tests to verify the business logic, I decided to test the command line arguments as well, just to make sure they’re parsed correctly and passed to their appropriate places in the business logic code. Initially I was thinking I needed to write a test that executes the actual command line such as python3 myscript.py –argument=value, but as this is neither appropriate nor really…

Read More Read More

Running Oracle in Docker for development and testing

Running Oracle in Docker for development and testing

I’m working on a Python based microservice that needs to communicate with an Oracle database. My development environment consist only of my laptop, so to do some real testing I decided to spin up Oracle in a Docker container on my laptop. For me, the process of getting things up and running wasn’t very straight forward for an Oracle newbie as myself, so I though I’d share my setup in case others may benefit from it. Let’s start with my…

Read More Read More

Extending business logic functions in unit tests

Extending business logic functions in unit tests

We have a database view, from now on referred to as View, that lists users we need to send an email to, and when we’ve sent the user an email we acknowledge this by updating the corresponding user in a database table (which is one of the tables the above mentioned database view is build from), from now on referred to as Table. For my unit tests I needed to emulate this behavior. I found that by extending the database…

Read More Read More

Spying on Python functions

Spying on Python functions

I was writing test code for a Python project, and needed to spy on the functions of one of the main object’s, called “server”, object instances called “rest_client”. By simply replacing the “rest_client” instance with a mock that passes the actual call to the wrapped object, I’m able to execute the production code of “rest_client” while later on making assertions.

Patching Python instance methods

Patching Python instance methods

I’m working on a project in which one of our services need to publish messages to a RabbitMQ exchange. Consider this code excerpt from the message queue client:

In my unit tests I wanted to verify that an unsuccessful attempt to publish the message, would cause trigger an exception to be thrown. To test this, I needed basic_publish  to return False. At first I found it tricky to patch instance methods such as basic_publish , but after coming across this excellent…

Read More Read More

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