test_verse() is a focused unit test for verse(). It calls the function with representative day or bottle values and compares each returned string with the required multiline output. section 11.1.3 section 13.1.1
A practical test_verse() follows a small, incremental pattern:
- Choose boundary and ordinary cases, such as verse(1) and verse(2). section 11.1.3
- Build expected output with '\n'.join([...]) and assert equality with the returned string. section 11.1.3
- Run pytest on the program, inspect failures, and refine verse() until every assertion passes. section 11.1.3
Testing two bottle verses
The test checks singular and plural behavior: verse(1) must end with “No more bottles,” while verse(2) must contain “2 bottles” followed by “1 bottle.” The same structure tests Christmas verses for days 1 and 2. section 11.1.3 section 13.1.3
def verse(bottle):
"""Sing a verse"""
return ''
def test_verse():
"""Test verse"""
last_verse = verse(1)
assert last_verse == '\n'.join([
'1 bottle of beer on the wall,', '1 bottle of beer,',
'Take one down, pass it around,',
'No more bottles of beer on the wall!'
])
two_bottles = verse(2)
assert two_bottles == '\n'.join([
'2 bottles of beer on the wall,', '2 bottles of beer,',
'Take one down, pass it around,', '1 bottle of beer on the wall!'
])
Shows the complete assertion structure for testing verse(1) and verse(2), including expected lines and newline joining. section 11.1.3
