After coming back to an old project, you tend to forget much of the practices you were doing, and may miss quite a few essential steps when implementing new functionality. Other than that, you may have just made an honest mistake, and BOOM, your backend comes crashing at your feet.
To avoid all that, backend developers write tests. Jumping into backend development myself, I wasn’t a big believer in unit test on the client side, but for the backend, it’s just essential to have some sort of testing in place.
Google App Engine Testing
I’ve gone through Google’s guide to testing your GAE application, and it is rather complicated and mundane. They show you how to write local unit tests for the logic of your application, and completely ignoring the datastore and top level handler testing.
After looking at my usecase, I determined that the best tests I can write are tests that act like a client connecting to the server, and go through all the APIs that the client can reach. This means, the tests will create a dummy client that logs in to the server, attempts to create a game, then plays a match, then checks the leaderboard, … etc. If all these tests pass, then my backend should be working perfectly.
For testing, I decided to go with pytest.
Writing the Tests
Granted the tests aren’t by any means fast, nor very well structured, but they work for my current requirements. I will need to structure them better later on.
Here is the general structure:
As you can see, first we test the account management stuff by registering a new user, attempting to login after that, then retrieving and updating the user info .. stuff like that.
Similarly, we do the same thing for testing leaderboards, rooms, and matchmaking. Here is some code for how the test code looks like:
You might notice there aren’t any assert statements? This is because the
Client class is a class created specifically for these tests, and it already has assert statements for the response code we get from the server. In other cases where we get custom data with the response, we have asserts in the test method:
If you notice, we have a decorator on one of the tests, which says “eventually consistent”. Due to GAE’s architecture, the data you write to the datastore may not be immediately available to all the clients. We say, this data is “eventually consistent”.
To accommodate this GAE restriction, I created this decorator that I decorate test cases that are eventually consistent. Test cases decorated with eventually consistent attempt the same test again, even if it fails the first time. Here is the Eventually consistent decorator:
The flaky marker has been imported from here.
I hope this was a useful post about testing in google app engine! I will hopefully post a follow up post about how to actually setup and teardown a testing GAE instance that can be used solely for the purpose of these tests.
I actually have even more things to say, like explain some useful pytest flags, and why I don’t terminate my tests automatically when they are done. (HINT: Examine the instance state before it terminates)