🤔 pytest

What is it? How to use it? What can I do with it?

#pytest

🧵Let's see👇
Although the Python standard library comes with a unit testing framework called ùnittest, pytest is the go-to testing framework for testing Python code.

https://docs.pytest.org/en/latest/ 

Let's see how to use it👇
1️⃣ To write and run the most simple example:

1. Install it via pip - "pip install pytest"
2. Creta a new file "simple_test.py" containing code from the image
3. Run it - "pytest simple_test.py"
2️⃣ You can also write tests inside the class.

It makes sense to use that when you want to wrap the test setup or complex assertions inside methods to make tests more readable
3️⃣ The best way to structure your tests and production code is as follows.

pytest.ini - configuration for pytest
http://conftest.py  - contains fixtures

tests - contains all tests
sum - package with production code (there can be multiple ones)
4️⃣ One fo great pytest's features are fixtures.

They are so great that they have their own thread: https://twitter.com/jangiacomelli/status/1343849489379909633?s=20
5️⃣ pytest.ini contains configuration like:

- pattern for searching test files
- pytest markers
- settings for plugins
6️⃣ You can easily parametrize your tests by using

- pytest.mark.parametrize decorator

The same test will run for multiple different inputs
7️⃣ There are a bunch of useful plugins to use with pytest:

- pytest-django - provides a set of tools made specifically for testing Django applications

- pytest-xdist - is used to run tests in parallel

- pytest-cov - adds code coverage support
8️⃣ When you don't want to run tests inside a specific folder you can use the option:

--ignore=some_folder

It can be used multiple times to exclude multiple folders.
1️⃣0️⃣ You can read much more about it here:

https://www.obeythetestinggoat.com/book/praise.harry.html
You can follow @jangiacomelli.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.