AUT: Cards (To-Do List)
pytest.fail(reason: str)
can be used to write custom checks.__tracebackhide__ == True
: assertion helper function won't be included to the traceback output.pytest.raises(expected_exception: Exception)
: expected exception.
Common pattern: separate test into stages.
-
Arrange - Act - Assert (Bill Wake, 2001, Kent Beck later popularized the practice as part of TDD)
-
Given - When - Then (Ivan Moore, BDD)
- Arrange / Given - A starting state. Set up data / env to get ready for the action.
- Act / When - Some action is performed.
- Assert / Then - Some expected result or end state should happen. At the end of the test, we make sure the action resulted in the expected behavior.
Subset | Syntax |
---|---|
Single test method | pytest path/test_module.py::TestClass::test_method |
All tests in a class | pytest path/test_module.py::TestClass |
Single test function | pytest path/test_moudle.py::test_function |
All tests in a module | pytest path/test_module.py |
All tests in a directory | pytest path |
Tests matching a name pattern | pytest -k pattern |
Tests by marker | pytest -m <mark> (Chapter 6) |