Writing test functions, fixtures, and assertions with the pytest framework, each explained with a short example.
30 cards · basic cards · AI-written, checked twice. Edit anything.
- In pytest, what must a test function name start with?
- test_
- What should pytest test files be named?
- test_*.py or *_test.py
- What is the basic syntax for an assertion in pytest?
- assert condition
- How do you assert that two values are equal in pytest?
- assert value1 == value2
- How do you test that a function raises an exception in pytest?
- with pytest.raises(ExceptionType):
- What is a pytest fixture?
- A function that provides test data or setup/teardown logic for tests
- What decorator marks a function as a pytest fixture?
- @pytest.fixture
- How do you use a fixture in a test function?
- Pass the fixture name as a parameter to the test function
- What is the default scope of a pytest fixture?
- function
- What does scope='class' mean for a pytest fixture?
- The fixture is created once per test class and reused across all methods in that class
- What does scope='module' mean for a pytest fixture?
- The fixture is created once per module and reused across all tests in that module
- What does scope='session' mean for a pytest fixture?
- The fixture is created once per test session and reused across all tests
- How do you add setup code to a fixture?
- Place code before the yield statement
- How do you add cleanup code to a fixture?
- Place code after the yield statement
- What is conftest.py in pytest?
- A special file where shared fixtures and configuration for a directory and subdirectories are defined