Skip to content
Timothy Ko edited this page Oct 24, 2018 · 3 revisions

Tests

Tests are a very important part of software development. Untested applications make it hard to improve existing code and creating new features without knowing whether you've regressed or not. Note that there should be integration tests if there are multiple services to your application. For the official documentation on Flask testing, look here. In addition, here's a good document on testing in python.

I've provided unit tests in the tests/ folder, which uses pytest to execute tests. The tests spins up a temporary postgres instance and connects the flask test client to it. This means that with the same code and same versions of dependencies, it should produce the same result. So, you can't have a persisting postgres database that you use in your tests because if you added a row to a Table, the tests may return a different result. Instead of stubbing SQLAlchemy, which is very cumbersome and difficult in my opinion, it's much easier to spin up a temporary database that SQLAlchemy would connect to. With testing, you want to figure out whether the code you wrote works, and generally when errors occur, it's probably not some complex database incompatibility issue, it's probably something that you wrote incorrectly.

To run the tests, do:

$ pipenv install --dev
$ pipenv run pytest tests

or do pipenv shell and blah blah blah