Testing Flask applications using Unittest in python

Testing Flask applications using Unittest in python

Testing Flask applications using Unittest in python

As in our earlier post, where we made a post about testing using pytest, Anything untested is broken, anyways lets leave this thesis for another day and focus more on how to be more productive about testing flask applications using unittest in python, now unit test is a module used for testing applications in python, but before we proceed, let’s understand what unit testing is.

What is Unit testing

As a developer writing applications or developing websites, there is a 100% certainty that after writing your programs you must test them, now how does this actually come through? it is either you test manually or you automate your tests, now how do we do this, manual testing has to do with self-clicking the links on a website which has already been deployed to check if they are not broken, it also involves scrutinizing several functions to check that they are working, but this is after the application has been deployed.
Inasmuch as manual testing is achievable, it is not efficient, and that is why automated testing is required, and that is where unit testing comes into Play. Unit testing is testing the individual functions of an application to see if they perform their individual functions.

The Testing Skeleton

We begin by adding a tests directory under the application root. Then create a Python file to store our tests (test_app.py). When we format the filename like test_*.py, it will be auto-discoverable by pytest.

Routing Tests?

Now for the purpose of this tutorial, we would write a sample code and then write tests for the code in order to see if they’ll function well without breaking, now we would just create a simple python file that would handle our routes and redirect them to a particular template for rendering, now how do we do it?

Create a simple file call it app.py

Then copy and paste the code below into it, the code is used to route basic functions to templates in flask, now although we’re simulating a real life example of a flask application here, we’re not building an application in its entirety, so let’s go further ahead, if you are looking for flask tutorials, kindly visit flask documentation Now the code:

from flask import Flask
from flask import url_for
from flask import render_template
app = Flask(__name__)
#redirect the routing functions to a particular view
@app.route('/')
def homepage():
    return render_template('index.html')
@app.route('/about')
def aboutpage():
    return render_template('about.html')
if __name__ = '__main__':
    app.run(debug=True)

Now let’s say that this is the file we want to test, now what are we testing, we’re trying to test, that the routes actually redirect to their proper pages and also that it sends a proper response to the server after its conquest.

Writing the Tests

Now since this is a simple test, we would just create a test file inside the same directory where the the main app.py file is, and then write our test codes, so just a quick one, create a file named test_app.py, and after you have done that copy and paste the code below (i advise that you familiarize your self with the codes).

# import the Os
import os
# import the unittest package for python
import unittest
# import app from the app.py file
from app import app

Now when writing tests, it is customary to set the testing environment up and also destroy the environment after the tests have been executed, so how do we do that, we just create functions that would do it.

# function to set up testing connection
def set_up(unittest.TestCase):
    app.config["TESTING"] = True
    app.config["DEBUG"] = True
    self.app = app.test_client()
    self.assertEqual(app.debug,False)
# function to teardown connection after testing
def tear_down():
    pass

Now we have written our functions to set up and also destroy the connections, so we are good to go with our tests, now we write the test that checkd for routing.

def homepage():
    response = self.app.get('/', follow_redirects = True)
    self.assertEqual(response.status_code, 200)
def aboutpage():
    response = self.app.get('/', follow_redirects = True)
    self.assertEqual(response.status_code, 200)
if __name__ == "__main__":
    unittest.main()

Now we put the code together and we should have what is below:

# import the Os
import os
# import the unittest package for python
import unittest
# import app from the app.py file
from app import app
class RoutingTests:
    # function to set up testing connection
    def set_up(unittest.TestCase):
         app.config["TESTING"] = True
         app.config["DEBUG"] = True
         self.app = app.test_client()
         self.assertEqual(app.debug,False)
    # function to teardown connection after testing
    def tear_down():
         pass
    def homepage():
         response = self.app.get('/', follow_redirects = True)
         self.assertEqual(response.status_code, 200)
   def aboutpage():
         response = self.app.get('/', follow_redirects = True)
         self.assertEqual(response.status_code, 200)
 if __name__ == "__main__":
     unittest.main()

Smith

I am both a front and backend developer

View all posts

Add comment

Your email address will not be published. Required fields are marked *

DMCA.com Protection Status