Writing functional tests with pytest

The pytest framework, along with its unique fixtures and the power of flask, allows us to write functional tests easily for our application. This allows us to test the API endpoints we have built with quite some ease.

Let's take a look at one of the sample tests for our index API endpoint, and then we will deep dive into how we wrote the test.

The following piece of code shows a sample test case written using pytest to test the index API endpoint:

'''
File: test_index_route.py
Description: Test the index API endpoint
'''
import os
import pytest
import sys
import tempfile

sys.path.append('.')
import bugzot

@pytest.fixture(scope='module')
def test_client():
db, bugzot.app.config['DATABASE'] = tempfile.mkstemp()
bugzot.app.config['TESTING'] = True
test_client = bugzot.app.test_client()

with bugzot.app.app_context():
bugzot.db.create_all()

yield test_client

os.close(db)
os.unlink(bugzot.app.config['DATABASE'])

def test_index_route(test_client):
resp = test_client.get('/')
assert resp.status_code == 200

This was a very simple functional test that we wrote for testing our index API route to see if it was working properly or not. Now, let's take a look at what we did here to get this functional test working:

The first few lines of code are more or less general, where we import some of the libraries that we will be requiring to build our tests.

The interesting work starts from the test_client() fixture we have built. The fixture is used to get us a flask-based test client that we can use to test our application endpoints to see if they are working correctly or not.

Since our application is a database-oriented application that will require a database to function correctly, the first thing we need to do is set up a database configuration for our application. For the purpose of testing, we can settle with an SQLite3 database that can be created quite easily in most of the operating systems. The following call provides us with the database we will be using for our testing purposes:

db, bugzot.app.config['DATABASE'] = tempfile.mkstemp()

The call returns a file descriptor to the database and a URI that we will store in the application config.

Once the database has been created, the next thing is to tell our application that it is running in a testing environment so that the error handling inside the application is disabled to improve the output of the tests. This is done easily by setting the TESTING flag inside the application configuration to True.

Flask provides us with a simple test client that we can use to run our application tests. This client can be obtained by making a call to the application test_client() method, as follows:

test_client = bugzot.app.test_client()

Once the test client is obtained, we need to set up the application context, which is done through calling the app_context() method of the Flask application.

With the application context established, we create our database by calling the db.create_all() method.

Once our application context is set up and a database has been created, the next thing we do is start the testing. This is achieved by yielding the test client:

yield test_client

Once this is done, the tests now execute and control transfers to the test_index_route() method where we simply try to load the index route by calling the get method of the test_client, as follows:

resp = test_client.get('/')

Once this is done, we check whether the API provided a valid response or not by checking the HTTP status code of the response and validating that it was a 200, SUCCESS or not, as follows:

assert resp.status_code == 200

Once the test finishes executing, the control transfers back to the fixture and we perform our cleanup by closing the database file descriptor and removing the database file, as follows:

os.close(db)
os.unlink(bugzot.app.config['DATABASE'])

Quite simple, wasn't it? That's how we can write a simple functional test with pytest and Flask. We can even write tests that handle the user authentication and database modifications this way, but we will leave this as an exercise for you as the reader.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.124