Build a Twitter Analytics App
2 The First Step: Design Your Solution
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
7 Adding the Data to a Database
8 Testing: What and How to Test
9 Displaying our Data using the Flask Webserver
9.2 Adding templates to our Flask app
9.3 Displaying our Tweets in the Flask Web Server
10 Future Work and Improvements
For the front end, which is our webserver running on Flask, we will write the tests first, in pure TDD fashion.
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
We need to write tests for these.
We will be using Selenium to test our browser. If you have never used it, Selenium is a cool tool to automate the browser. We’ll be using it with Unittest.
The code is here.
Let’s look at the very first test:
1 2 3 4 5 6 7 |
class TestFlask(unittest.TestCase): def test_web_app_running(self): try: r = requests.get("http://127.0.0.1:5000/") except: self.fail("Could not open web app. Not running, or crashed. Test Failed") |
This is a very basic test, but very important.
It checks if our wbserver is running at all. You might have made a minor change that kills your webapp completely; so the first thing to do is test that the server is running. If our test can’t find a running server at http://127.0.0.1:5000/, which is where the development server runs, it will exit.
Next, we will test the features one by one.
The first is to test the languages on our page.
1 2 3 4 5 6 7 |
def test_lang(self): r = requests.get("http://127.0.0.1:5000/") page_src = r.text if page_src.find("Most used languages on Twitter: All Tweets") < 0: self.fail("Can't find most common languages") |
We get our webserver page, and search for the text Most used languages on Twitter: All Tweets in it. If we can’t find it, the test fails.
A very basic test, but it will show our app is up and running. The next test is to check we can view the top tweets:
1 2 3 4 5 6 7 8 9 |
def test_top_tweets(self): r = requests.get("http://127.0.0.1:5000/top_tweets") page_src = r.text if page_src.find('Most Popular Tweets') < 0: self.fail("Top tweets failed") if page_src.find('<blockquote class="twitter-tweet') < 0: self.fail("Can't find the top tweets") |
Same as before, we search for the strong Most popular tweets. This time, we have an extra search: blockquote class=”twitter-tweet
This is how the embedded tweets start. So this time, we are looking for some embedded tweets in the page, in addition to our text.
Finally, we test the trends:
1 2 3 4 5 6 7 8 9 |
def test_trends(self): r = requests.get("http://127.0.0.1:5000/trends") page_src = r.text if page_src.find('Trending On Twitter:') < 0: self.fail("Trends failed") if page_src.find('<blockquote class="twitter-tweet') < 0: self.fail("Can't find the trending tweets") |
Run the tests. They should all fail. This is expected, as we don’t have any code for our webserver yet.
Code Review
The tests are very basic, and are not testing everything. For example, we are testing that the languages are there on the page, but not if the graphs are. Similarly, we are only searching for one embedded tweet.
While basic, these tests are good enough for us to get started with. Let’s write some backend tests now.