4 ms·
Nice sample of tests! I also love pytest, but one of the best things IMO is fixtures, which your tests don't seem to use? For those who don't know fixtures are
by orf 10y ago
Nice sample of tests! I also love pytest, but one of the best things IMO is fixtures, which your tests don't seem to use?
For those who don't know fixtures are a type of dependency injection. You just make a function (e.g game) in a file called conftest.py, and wrap it with a '@pytest.fixture'. Then any tests you write that have an argument named 'game' will have the result of the 'game' function passed as a value.
So 'game' could return 'prepare_game()', and other fixtures can do the same with their arguments to build more complex fixtures. I find it saves a load of initialization code being repeated.
- nilliams 10y agoFixtures are considered an anti-pattern by many as they over-DRY [0] your tests and obscure them by introducing 'Mystery Guests' [1]. [0] http://stackoverflow.com/a/11837973 http://stackoverflow.com/a/11837973 [1] https://robots.thoughtbot.com/lets-not#will-our-mystery-guest-please-leave https://robots.thoughtbot.com/lets-not#will-our-mystery-gues...
- scrollaway 10y agoYeah, I don't actually make deep use of pytest functionality. The whole thing used to be runnable without pytest for ease of use (with a __main__ block finding and running all test() functions). I should definitely use fixtures - been meaning to! It's why I love pytest, you don't need to go deep into it to make a high quality test suite.
- nilliams 10y agoPlease don't move to fixtures, your tests are great as-is (thanks for sharing). See my comment above!
- scrollaway 10y agoI agree with it and hold the same view, which is why I haven't moved to them yet. My plan was to get rid of the "game = prepare_game(...)" line that's present on every single test, and have the game be passed as an argument to the tests which AIUI pytest supports.