Testing Our Backend

Build a Twitter Analytics App

1 Introduction: Start Here

2 The First Step: Design Your Solution

3 In Which I Rant a Little

4  Design Solution

5 Writing Great Code

6 Writing the Backend Twitter Server

Writing the Code in Small Parts: Part 1, The Basic App

Part 2: Adding a Counter to Exit

Part 3: Adding Language and Retweet Count

Part 4: Organising Our Code

7 Adding the Data to a Database

8 Testing: What and How to Test

8.1 Testing Our Frontend

8.2 Testing Our Backend

9 Displaying our Data using the Flask Webserver

9.1 Introduction to Flask

9.2 Adding templates to our Flask app

9.3 Displaying our Tweets in the Flask Web Server

10 Future Work and Improvements

Let’s remind ourselves. What does our user expect on the page?

  • There will be some graphs with the languages and top languages statistics
  • They will see the top tweets
  • They will see the trends on Twitter

Now, all of these are webserver tests. However, they also affect the backend, as the backend produces these statistics. So these tests will need to be repeated for the backend.

But this won’t be hard; if you remember, we refactored our code, so that all the above features have their own functions now. All we need¬† to do is write tests for them.

Let’s have a look. The code is here.

We are using the Python Unittest library.

The setUp() function is called before running any tests.

The first thing we do is create a Sqlite database, this time in memory only. This will test the actual database code, without messing up our live database.

We create the tables we need.

And we create our Twitter class. We are only grabbing 5 tweets, as it’s good enough to prove our tests work. And the tests will be fast!

There are two helper functions to read data from our database:


Okay, to test for our features:

The tests are quite simple. I call the functions to gather the tweets and trends, and then check they have been written to the database. We don’t check what the actual content is, because that is not predictable. As long as something was written to our database, our tests will pass. This could be improved, of course.

We have one more test. It is not directly needed, but we do need to test it to ensure our code has no bugs. This is the stats class. The test for it is quite simple.

We just write some data, and read it back:

Finally, at the end of our tests, we tear down our database.

Next: We start writing our Flask webserver