liveBook · Manning

Test_verse()

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A book-specific test function that checks the output or behavior of the verse() function for selected day numbers.

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

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Defines assertions, expected multiline strings, test discovery, and failure-driven refinement, but not broader testing theory.
Examples
Tests bottle counts 1 and 2, then tests Christmas days 1 and 2 with exact expected lyrics.
What it assumes
Assumes functions, return values, strings, lists, and assertions while introducing function-level tests and pytest discovery.
What you can do after
Diagnose/debug with pytest by comparing actual and expected verse strings and iterating until the assertions pass.
How it connects
Connects verse(), pytest, unit testing, assertions, and string joining.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.