Prompt
Structure test cases for maximum readability and maintainability by following these principles:
- Organize each test in three distinct parts separated by blank lines:
- Setup (arrange test fixtures)
- Execution (run unit under test)
- Verification (assert expected results)
- Minimize intermediate variables - use direct assertions when possible:
Instead of:
matrix = [["cell1", "cell2"], ["cell3", "cell4"]]
expected_html = "<table><tr><td>cell1</td>...</table>"
assert utils.htmlify_matrix(matrix) == expected_html
Prefer:
assert utils.htmlify_matrix([["cell1", "cell2"], ["cell3", "cell4"]]) == (
"<table><tr><td>cell1</td><td>cell2</td></tr>"
"<tr><td>cell3</td><td>cell4</td></tr></table>"
)
- Use parameterization to avoid repeated test code:
@pytest.mark.parametrize( "date", ["1990-12-01", "2050-01-01T00:00:00", "2050-01-01+00:00:00"] ) def test_validate_date_args_accepts_standard_formats(date): assert utils.validate_date_args(date) - Write meaningful assertions that clearly indicate the behavior being tested:
```python
Weak assertion:
assert “error” in caplog.text
Better assertion:
assert “invalid date format, expected YYYY-MM-DD” in caplog.text ```