pytest is the dominant Python test runner—install locally with pip. Tests are plain functions named test_* with assert statements—simpler than Java JUnit boilerplate for many teams.
Example test (local)
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
pip install pytest
pytest -q
Simulating tests in playground
The sandbox cannot run pytest CLI—assert in functions to practice Arrange-Act-Assert thinking before local pytest setup.
Important interview questions and answers
- Q: pytest vs unittest?
A: unittest is stdlib xUnit style; pytest has simpler asserts and rich plugin ecosystem. - Q: Arrange-Act-Assert?
A: Setup data, invoke behavior, verify outcome—readable test structure.
Self-check
- What prefix names a pytest test function?
- What CLI runs pytest locally?
Tip: Name tests test_feature_scenario_expected—run pytest -q locally after pip install pytest.
Interview prep
- pytest vs unittest?
unittest stdlib xUnit style; pytest simpler asserts and plugin ecosystem—both valid.
- Arrange-Act-Assert?
Setup, invoke, assert—readable structure for maintainable tests.