|
| 1 | +# Days 10-12 Test your code with pytest |
| 2 | + |
| 3 | +In this lesson I show you how to use pytest to test a simple guessing game. It might not be the easiest example for a beginner, but it allows me to show some real world issues you want to address in your test code. |
| 4 | + |
| 5 | +After pip installing the module, I quickly show you how to write a test with pytest and how it differs from the classic (more verbose) unittest syntax. |
| 6 | + |
| 7 | +Next I show you some tactics to mock out user inputs and random data, because test data needs to be predictable. We also learn how we can capture/test standard output of our program. |
| 8 | + |
| 9 | +Finally I show you some TDD or _test driven development_ in action by implementing Fizz Buzz by writing the tests first, in small incremental steps. You learn about the `pytest.mark.parametrize` decorator to elegantly handle repetitive tests. |
| 10 | + |
| 11 | +The pytest framework is huge and this is just a subset of features. I hope this gives you a head start though to start writing (more) tests for your programs to produce more reliable software. |
| 12 | + |
| 13 | +Be warned: mastering pytest might feel like possessing a super power! |
| 14 | + |
| 15 | +## Day N: Setup + Learn pytest! |
| 16 | + |
| 17 | +Today you will pip install pytest and pytest-cov and watch the video lectures. |
| 18 | + |
| 19 | +Start thinking about how you can write tests for your code ... |
| 20 | + |
| 21 | +## Day N+1: Your Turn: Test your code! |
| 22 | + |
| 23 | +Head over to [PyBites Code Challenge 39 - Writing Tests With Pytest](https://codechalleng.es/challenges/39/) and start adding tests to your code or if you're already covered maybe you want to do that as contribution to an open source project? |
| 24 | + |
| 25 | +By the way, notice that we use pytest for [our Bites](https://codechalleng.es/bites/) too. Under the _TESTS_ tab of each Bite you can see how your code will be tested and when you hit _Save + verify_ you can look at its output. |
| 26 | + |
| 27 | +Lastly if you are serious about writing tests and pytest check out Brian Okken's [Test and Code](http://testandcode.com) podcast and his [Python Testing with pytest](https://pragprog.com/book/bopytest/python-testing-with-pytest) ([our review](https://pybit.es/pytest-book.html)), which goes into much more depth. |
| 28 | + |
| 29 | +## Day N+2: Your Turn: Write a fixture! |
| 30 | + |
| 31 | +You wrote some test code? Good, hope that felt good. I know it does because with a set of tests you have more confidence to make any changes in the future. Software systems become increasingly complex so it's paramount to have a suite of tests as your project grows to catch any regression bugs. |
| 32 | + |
| 33 | +On the topic of more complex code, one thing I did not cover are pytest fixtures: |
| 34 | + |
| 35 | +> 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 - [pytest fixtures: explicit, modular, scalable](https://docs.pytest.org/en/latest/fixture.html) |
| 36 | +
|
| 37 | +A typical example is a database app that needs to setup and tear down its state before each test. Read through and [pytest fixtures easy example](http://pythontesting.net/framework/pytest/pytest-fixtures-easy-example/) and try to find a use case for the `pytest.fixture` decorator yourself. You can do it! |
| 38 | + |
| 39 | +Let me give you another example: I used it [for this DB app](https://realpython.com/blog/python/building-a-simple-web-app-with-bottle-sqlalchemy-twitter-api/#add-tests-with-pytest) ([code](https://github.com/pybites/pytip/blob/master/tests/test_tips.py)). |
| 40 | + |
| 41 | +Ready to become a pytest ninja? |
| 42 | + |
| 43 | +### Time to share what you've accomplished! |
| 44 | + |
| 45 | +Be sure to share your last couple of days work on Twitter or Facebook. Use the hashtag **#100DaysOfCode**. |
| 46 | + |
| 47 | +Here are [some examples](https://twitter.com/search?q=%23100DaysOfCode) to inspire you. Consider including [@talkpython](https://twitter.com/talkpython) and [@pybites](https://twitter.com/pybites) in your tweets. |
| 48 | + |
| 49 | +*See a mistake in these instructions? Please [submit a new issue](https://github.com/talkpython/100daysofcode-with-python-course/issues) or fix it and [submit a PR](https://github.com/talkpython/100daysofcode-with-python-course/pulls).* |
0 commit comments