loading...

Any tutorials to recommend about testing in Python?

twitter logo github logo ・1 min read

I've been building my little application using Flask and Python. I feel pretty confident building it, except for testing.

Testing is topic still unclear to me... I am not sure what should I test, when, or how. I've read some articles, but it is still a blurry topic.

So, do you have any interesting testing tutorials, articles, resources, that have helped you in the past?

Thanks!!
Daniel

twitter logo DISCUSS (5)
markdown guide
 

Hey Daniel!

I would start with Dive Into Python's chapter about unit testing to have a general idea about what testing is.

Since you have a web application (but it applies to most apps) you can probably have three types of testing:

  • unit tests
  • functional/integration tests
  • UI tests

In unit tests you can test things like: "if I give 1 and 2 to a method call sum() I should get 3 as the return value". You test units of code and methods that should not have side effects. You can go far with just the module unittest to write tests. I suggest using pytest to actually run them because it can discover them in subdirectories.

In functional and integration tests you test behavior. What happens if hit the POST /publish endpoint? Is it calling a third party API? Is it returning the correct response? You can use vcrpy to record interactions with third parties. These tests are also where you test interactions with the database. I wouldn't bother testing the content of the HTML pages unless strictly necessary, there are better tools for that. You can setup Flask tests with this guide: flask.pocoo.org/docs/0.12/testing/ and you can use Flask-Testing to make your life easier: pythonhosted.org/Flask-Testing/

You can separate both types of tests using subfolders, like tests/unit and test/functional or something like that so you can run them separately if you need to.

Testing the actual UI is trickier and I wouldn't use Python's tools at all. Consider your apps as black box, because your user is not going to care about what language is written into. So you can start up the server and use a tool like Cypress.io or TestCafe to help you do that.

Hope I clarified some topics, start with the basics :-)

 
 
Classic DEV Post from Jul 5 '19

What's one thing you wish you knew before you started programming?

Daniel da Rocha profile image
architect turned dev