laurids
laurids

Reputation: 941

How to properly unit test a web app?

I'm teaching myself backend and frontend web development (I'm using Flaks if it matters) and I need few pointers for when it comes to unit test my app.

I am mostly concerned with these different cases:

  1. The internal consistency of the data: that's the easy one - I'm aiming for 100% coverage when it comes to issues like the login procedure and, most generally, checking that everything that happens between the python code and the database after every request remain consistent.

  2. The JSON responses: What I'm doing atm is performing a test-request for every get/post call on my app and then asserting that the json response must be this-and-that, but honestly I don't quite appreciate the value in doing this - maybe because my app is still at an early stage?

    • Should I keep testing every json response for every request?
    • If yes, what are the long-term benefits?
  3. External APIs: I read conflicting opinions here. Say I'm using an external API to translate some text:

    • Should I test only the very high level API, i.e. see if I get the access token and that's it?
    • Should I test that the returned json is what I expect?
    • Should I test nothing to speed up my test suite and don't make it dependent from a third-party API?
  4. The outputted HTML: I'm lost on this one as well. Say I'm testing the function add_post():

    • Should I test that on the page that follows the request the desired post is actually there?
    • I started checking for the presence of strings/html tags in the row response.data, but then I kind of gave up because 1) it takes a lot of time and 2) I would have to constantly rewrite the tests since I'm changing the app so often.
    • What is the recommended approach in this case?

Thank you and sorry for the verbosity. I hope I made myself clear!

Upvotes: 3

Views: 1653

Answers (1)

Douglas Adams
Douglas Adams

Reputation: 1550

Most of this is personal opinion and will vary from developer to developer.

  1. There are a ton of python libraries for unit testing - that's a decision best left to you as the developer of the project to find one that fits best with your tool set / build process.

  2. This isn't exactly 'unit testing' per se, I'd consider it more like integration testing. That's not to say this isn't valuable, it's just a different task and will often use different tools. For something like this, testing will pay off in the long run because you'll have piece of mind that your bug fixes and feature additions aren't impacting your end to end code. If you're already doing it, I would continue. These sorts of tests are highly valuable when refactoring down the road to ensure consistent functionality.

  3. I would not waste time testing 3rd party APIs. It's their job to make sure their product behaves reliably. You'll be there all day if you start testing 3rd party features. A big reason to use 3rd party APIs is so you don't have to test them. If you ever discover that your app is breaking because of a 3rd party API it's probably time to pick a different API. If your project scales to a size where you're losing thousands of dollars every time that API fails you have a whole new ball of issues to deal with (and hopefully the resources to address them) at that time.

  4. In general, I don't test static content or html. There are tools out there (web scraping tools) that will let you troll your own website for consistent functionality. I would personally leave this as a last priority for the final stages of refinement if you have time. The look and feel of most websites change so often that writing tests isn't worth it. Look and feel is also really easy to test manually because it's so visual.

Upvotes: 5

Related Questions