Test a movie db class
Level: Advanced (score: 4)
We moved from mutpy
(<= 3.7) to mutatest
(3.12 📈) for test Bites.
First we check if you have 90% test coverage, then we inject some mutations into the code to see if your tests catch them. 🤯
Good luck 🔥 💪 (please be patient 🙏 - it can take a few seconds to run the tests 🕒)
Time for fixtures, one of pytest
's killer features or as Brian Okken said: pytest fixtures are one of the unique core features that make pytest
stand out above other test frameworks, and are the reason why many people switch to and stay with pytest
(Python Testing with pytest).
Why fixtures? The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions (docs).
In this Bite we wrote a little movie DB using sqlite3
. The idea is that you write a db
fixture to instantiate this class and run its init
method to get the movies table created and populated. At the end of the fixture you use drop_table
to undo any changes.
Apart from this setup and teardown there are 3 methods you need to test: query
, add
and delete
. Specially query
has various args, so make sure you test them all.
Apart from the docs we also wrote an article on fixtures you might want to check out: All You Need to Know to Start Using Fixtures in Your pytest Code.
Let us know on Twitter when you finish our pytest
learning path ... Good luck and keep calm and code in Python / pytest
!