Why do test patterns help improve test quality when using pytest?
Think about how consistency helps when many people read or update tests.
Test patterns provide a clear and consistent way to write tests. This makes tests easier to understand and maintain, which improves overall test quality.
What is the output when running this pytest test using a fixture pattern?
import pytest @pytest.fixture def sample_data(): return [1, 2, 3] def test_sum(sample_data): assert sum(sample_data) == 6 def test_len(sample_data): assert len(sample_data) == 3
Consider what the fixture returns and how it is used in both tests.
The fixture sample_data returns a list [1, 2, 3]. The sum is 6 and length is 3, so both assertions pass.
Which pytest assertion correctly tests that a function raises a ValueError when given bad input?
def bad_func(x): if x < 0: raise ValueError("Negative not allowed") return x
Remember how pytest checks for exceptions using context managers.
Option C uses pytest.raises context manager correctly to check for the exception. Other options misuse assertions or functions.
What is the bug in this pytest test code that uses a fixture pattern?
import pytest @pytest.fixture def data(): return [1, 2, 3] def test_data_sum(): assert sum(data) == 6
Think about how pytest passes fixture values to test functions.
The test function must accept the fixture as a parameter to use its returned value. Without it, data is undefined in the test.
In pytest, which pattern best isolates tests that share setup but modify data independently?
Consider how to avoid tests affecting each other's data.
Using a function-scoped fixture ensures each test gets a fresh copy of data, preventing side effects between tests. Other options share data and risk interference.