context manager
An object that sets something up and later tears it down, designed to be used with a with statement.
In more detail
Files, locks and database connections are common context managers. The clean-up runs however the block is left, normally or through an exception.
Examples
open() returns a file object that is also a context manager.with pytest.raises(ValueError):
discounted(1000, 120)Related terms
- with statement — A statement that sets something up, runs a block, and guarantees the matching clean-up afterwards, most often closing a file.
- pytest.raises — A pytest helper, used in a with block, that makes a test pass only if the code inside raises the named exception.
- setup and teardown — The work done before a test to prepare what it needs, and after it to clean up, such as closing files or stopping containers.
Taught in
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book
- Introducing pytest — Automated Testing, TDD & Quality Engineering
- Testing The Filesystem — Automated Testing, TDD & Quality Engineering