Junior — Middle
What approaches and standards do you use when creating test scenarios for Python code?
sobes.tech AI
Answer from AI
When creating test scenarios for Python, I adhere to the following approaches and standards:
- Use frameworks like unittest or pytest for organizing tests.
- Write tests that cover both positive and negative scenarios.
- Follow the "Arrange-Act-Assert" principle for clarity.
- Use fixtures to prepare test data and environment.
- Write independent tests so they can be run in any order.
- Use mock objects to isolate tested components.
- Strive for good code coverage without compromising test quality.
- Follow PEP 8 for readability of test code.
Example of a simple test with pytest:
import pytest
def add(a, b):
return a + b
def test_add_positive():
assert add(2, 3) == 5
def test_add_negative():
assert add(-1, -1) == -2
This approach helps create maintainable and reliable tests.