Step 4 of 7 Write tests first, even if the course does not require them
Tests are the green/red signal that tells you whether the last 20 minutes of typing helped
or hurt. Write 3 to 5 pytest cases from the example I/O in the brief BEFORE the
implementation. Run them: they fail with NameError
or AssertionError. Now you have a target. This is
TDD-lite for coursework and it cuts debug time roughly in half.
tests/test_merge.py
# tests/test_merge.py
# Requires: pip install pytest
import pytest
from merge import merge_sorted
def test_basic_interleave():
assert merge_sorted([1, 3, 5], [2, 4, 6]) == [1, 2, 3, 4, 5, 6]
def test_empty_left():
assert merge_sorted([], [2, 4, 6]) == [2, 4, 6]
def test_empty_right():
assert merge_sorted([1, 3, 5], []) == [1, 3, 5]
def test_both_empty():
assert merge_sorted([], []) == []
def test_duplicates_across_lists():
assert merge_sorted([1, 2, 2, 3], [2, 2, 4]) == [1, 2, 2, 2, 2, 3, 4]
@pytest.mark.parametrize("a, b, expected", [
([0], [0], [0, 0]),
([-3, -1], [-2, 0], [-3, -2, -1, 0]),
([1, 1, 1], [1, 1], [1, 1, 1, 1, 1]),
])
def test_parametrized_cases(a, b, expected):
assert merge_sorted(a, b) == expected