Write a unit test in Python with pytest for a function called mergeSpans that takes a list of time ranges and returns the merged set. Include cases for an empty list, touching ranges, and out-of-order input.
Ah, the unit test — the quiet workhorse behind every release that doesn't wake somebody at 03:14. Let's put this mergeSpans helper through its paces with a proper battery of edge cases.
Here's a compact, pytest-flavoured suite for your interval merger:
python
import pytest from timespans import mergeSpans # replace 'timespans' with your real module path def test_merge_spans_empty_list(): """Test that an empty list merges to nothing.""" assert mergeSpans([]) == [] def test_merge_spans_touching_ranges(): """Test that ranges sharing an edge collapse.""" assert mergeSpans([(0, 3), (3, 8)]) == [(0, 8)] def test_merge_spans_out_of_order(): """Test that unsorted input is sorted before merging.""" assert mergeSpans([(5, 9), (0, 2)]) == [(0, 2), (5, 9)]
Ask anything
Marline can make mistakes. Check important info.