Friday, August 19, 2016

Adding test teardown.

I want to add a teardown method to my tests.  We've just been posting tweets, and not cleaning them up!  Frankly my reason for doing this is because the return json from getting my account timeline is huge.


First we have to add a "delete" tweet method to our service class:

   DESTROY_STATUS_URL = "/1.1/statuses/destroy/{status_id}.json"  
   
 ...  
   
   def delete_status(self, status_id):  
     url = self.DESTROY_STATUS_URL.format(status_id=status_id)  
     return self._post(url, None)  
   

I was surprised they used a POST, instead of a DELETE command.  In this case the POST has no payload.  The id is inserted into the url path.

Now we can add the teardown_class clause in our test suite.

   def teardown_class(self):  
     self.logger.info("Teardown: Remove all but 2 tweets")  
     response = self.twitter_service.get_home_timeline()  
     TestHelpers.verify_http_response(response, 200, "Get Twitter Account Timeline")  
     for x in range(0,len(response.response_json) - 2):  
       self.twitter_service.delete_status(response.response_json[x]["id"])  
     response = self.twitter_service.get_home_timeline()  
     TestHelpers.verify_http_response(response, 200, "Get Twitter Account Timeline")  
     self.logger.debug("Number of remaining tweets: {}".format(len(response.response_json)))  

This will remove all but 2 tweets from my timeline.  I suppose once I know my test cleanup is running I can remove the last 2 lines.  Also note: Yes this uses an as-of-yet untested method in our API.  We'll create a test case for delete.

I didn't make a branch for this small change. We'll roll this into the changes made for data classes.

No comments:

Post a Comment