pytest is a Python testing framework for discovering and running tests with concise, readable assert statements. It helps check that new features work and that existing behavior remains correct after changes. section 5.0 section 5.5
Use pytest to keep tests straightforward and easy to organize:
- Let pytest discover tests automatically, including existing unittest tests. section 5.5.1 section 5.5.2
- Name test classes with the Test prefix; inheritance from unittest.TestCase is unnecessary. section 5.5.1
- Write ordinary assert expressions instead of unittest assertion methods. section 5.5 section 5.5.2
A small pytest test
class TestProduct:\n def test_generate_sku(self):\n product = Product('shoes', 'S', 'black')\n assert product.generate_sku() == 'SHOES-S-BLACK'\n\nRun the suite with pytest. The command reports the platform, collected tests, individual results, and an overall summary. section 5.5.1 section 5.5.2
$ pytest ========== test session starts ========== platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, #1 pluggy-0.12.0 rootdir: /path/to/ecommerce/project collected 15 items #2 test_cart.py ............ [ 80%] #3 test_product.py .. [ 93%] test_tax.py . [100%] ======= 15 passed in 0.12 seconds ======= #4
Inspect a sample pytest session showing automatic collection, progress, individual test files, and the final pass count. section 5.5










