telephone

Part of Computer Programming is sending input data to a process and getting output data back

input_object -> process -> output_object

I send things (input data) to a program to test it, and check if what I think will happen (my expectation) is the same as the results I get (reality). This helps me answer two questions:

  • what is the same?

  • what is different?

The difference helps me know what to change to get what I want. I use assertions to test if reality is the same as my expectation.

assert reality == my_expectation

where

  • reality is what happens when I do something with code

  • my expectation is what I think will happen when I do something with code

The exercises in this chapter show how I can pass objects to a function and use it to make a string (anything in quotes).


preview

I have these tests by the end of the chapter

telephone/tests/test_telephone.py
 1def assert_equal(a, b):
 2    assert a == b
 3
 4
 5def text(the_input):
 6    return f'I got: {the_input}'
 7
 8
 9def test_passing_none():
10    assert_equal(text(None), 'I got: None')
telephone/tests/test_telephone.py
13def test_passing_booleans():
14    assert_equal(text(False), 'I got: False')
15    assert_equal(text(True), 'I got: True')
16
17
18def test_passing_an_integer():
19    an_integer = 1234
20    assert_equal(text(an_integer), f'I got: {an_integer}')
21
22
23def test_passing_a_float():
24    a_float = 5.678
25    assert_equal(text(a_float), f'I got: {a_float}')
telephone/tests/test_telephone.py
28def test_passing_a_string():
29    a_string = 'hello'
30    assert_equal(text(a_string), f'I got: {a_string}')
31
32
33def test_passing_a_tuple():
34    a_tuple = (0, 1, 2, 'n')
35    assert_equal(text(a_tuple), f"I got: {a_tuple}")
36
37
38def test_passing_a_list():
39    a_list = [0, 1, 2, 'n']
40    assert_equal(text(a_list), f'I got: {a_list}')
telephone/tests/test_telephone.py
43def test_passing_a_set():
44    a_set = {0, 1, 2, 'n'}
45    assert_equal(text(a_set), f'I got: {a_set}')
46
47
48def test_passing_a_dictionary():
49    a_dictionary = {
50        'key0': 'value0',
51        'keyN': [0, 1, 2, 'n'],
52    }
53    reality = text(a_dictionary)
54    my_expectation = f'I got: {a_dictionary}'
55    assert_equal(reality, my_expectation)
telephone/tests/test_telephone.py
58def test_passing_a_class():
59    assert_equal(
60        text(object), "I got: <class 'object'>"
61    )
62    assert_equal(text(bool), "I got: <class 'bool'>")
63    assert_equal(text(int), "I got: <class 'int'>")
64    assert_equal(text(float), "I got: <class 'float'>")
65    assert_equal(text(str), "I got: <class 'str'>")
66    assert_equal(text(tuple), "I got: <class 'tuple'>")
67    assert_equal(text(list), "I got: <class 'list'>")
68    assert_equal(text(set), "I got: <class 'set'>")
69    assert_equal(text(dict), "I got: <class 'dict'>")
70
71
72# Exceptions seen
73# AssertionError
74# NameError
75# TypeError

start the project

  • I name this project telephone

  • I open a terminal

  • I use uv to make a directory for the project and initialize it

    uv init telephone
    

    the terminal shows

    Initialized project `telephone`
    at `.../pumping_python/telephone`
    

    then goes back to the command line.

  • I change directory to the project

    cd telephone
    

    the terminal shows I am in the telephone folder

    .../pumping_python/telephone
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line.

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    

    the terminal goes back to the command line.

  • I make an empty file for the tests in the tests folder

    touch tests/test_telephone.py
    
    New-Item tests/test_telephone.py
    

    the terminal goes back to the command line.

  • I open test_telephone.py from the tests folder

  • I add the first failing test to test_telephone.py

    1def test_failure():
    2    assert False is True
    
  • I go back to the terminal

  • I use echo to add pytest to requirements.txt

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use Out-File to add pytest to requirements.txt

    "pytest" | Out-File requirements.txt -Encoding UTF8
    
  • I add pytest-watcher to the requirements file

    "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I add the new files and folders to git for tracking

    git add .
    

    the terminal goes back to the command line.

  • I add a git commit message

    git commit --all --message 'setup project'
    

    the terminal shows a summary of the changes then goes back to the command line.

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES ========================
    ______________________ test_failure ______________________
    
        def test_failure():
    >       assert False is True
    E       assert False is True
    
    test_telephone.py:2: AssertionError
    ================ short test summary info =================
    FAILED test_telephone.py::test_failure - assert False is True
    =================== 1 failed in X.YZs ====================
    

    because False is NOT True.

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now again

  • I add AssertionError to the list of Exceptions seen, to test_telephone.py

    1def test_failure():
    2    assert False is True
    3
    4
    5# Exceptions seen
    6# AssertionError
    
  • I change True to False in the assertion

    1def test_failure():
    2    # assert False is True
    3    assert False is False
    4
    5
    6# Exceptions seen
    7# AssertionError
    

    the test passes.


test_passing_none

Can I pass None (the simplest object) as input to a function?


RED: make it fail



GREEN: make it pass


  • I add the function

    1def test_passing_none():
    2    def text():
    3        return None
    4
    5    assert text(None) == 'I got: None'
    6
    7
    8# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    TypeError: test_passing_none.<locals>.text()
               takes 0 positional arguments but 1 was given
    

    because the assertion called the text function which belongs to test_passing_none with input (None) and the function definition does not allow any inputs, the parentheses are empty.

  • I add TypeError to the list of Exceptions seen

     8# Exceptions seen
     9# AssertionError
    10# NameError
    11# TypeError
    
  • I add a name to the function definition

    1def test_passing_none():
    2    # def text():
    3    def text(the_input):
    4        return None
    5
    6    assert text(None) == 'I got: None'
    7
    8
    9# Exceptions seen
    
    • the_input is the name I used for the input, I can use any name I want.

    • the terminal is my friend, and shows AssertionError

      E       assert None == 'I got: None'
      

    because the assertion expects 'I got: None' and the text function returns None.

  • I copy the string from the terminal and paste it in the return statement to replace None

     1def test_passing_none():
     2    # def text():
     3    def text(the_input):
     4        # return None
     5        return 'I got: None'
     6
     7    assert text(None) == 'I got: None'
     8
     9
    10# Exceptions seen
    

    the test passes.

    text(None) -> 'I got: None'
    └── def text(the_input):
        └── return 'I got: None'
    

REFACTOR: make it better


  • I remove the commented lines from test_passing_none

    1def test_passing_none():
    2    def text(the_input):
    3        return 'I got: None'
    4
    5    assert text(None) == 'I got: None'
    6
    7
    8# Exceptions seen
    
  • I open a new terminal then change directories to telephone

    cd telephone
    

    the terminal shows I am in the telephone folder

    .../pumping_python/telephone
    
  • I add a git commit message in the new terminal

    git commit -am 'add test_passing_none'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass None as input to a function.

The problem with this solution is that the text function does not care about what it gets, it always returns 'I got: None' when it is called. I want it to return the object it gets as part of the string.


test_passing_booleans

Can I pass booleans from a test to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for booleans (there are only two), first with an assertion for False, to test_telephone.py

     1def test_passing_none():
     2    def text(the_input):
     3        return 'I got: None'
     4
     5    assert text(None) == 'I got: None'
     6
     7
     8def test_passing_booleans():
     9    assert text(False) == 'I got: False'
    10
    11
    12# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'text' is not defined
    

    because the text function belongs to the test_passing_none function and I cannot reach it from outside.


GREEN: make it pass


  • I move the text function out of test_passing_none so that it can be called from anywhere in the file

    1def text(the_input):
    2    return 'I got: None'
    3
    4
    5def test_passing_none():
    6    assert text(None) == 'I got: None'
    7
    8
    9def test_passing_booleans():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: None'
                        == 'I got: False'
    

    because the text function always returns 'I got: None' and this assertion expects 'I got: False'.

  • I change the return statement to give the test what it wants

    1def text(the_input):
    2    # return 'I got: None'
    3    return 'I got: False'
    4
    5
    6def test_passing_none():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: False' == 'I got: None'
    

what is string interpolation?

String Interpolation is how to place objects in strings. It allows me to make one string that can have values that change.

I can use an f-string (short for formatted string literal) for string interpolation.

A string is anything inside quotes, for example

  • 'single quotes'

  • '''triple single quotes'''

  • "double quotes"

  • """triple double quotes"""


how to write an f-string


f'characters {object} more characters'
  • I add the_input to the string in the return statement of the text function

    1def text(the_input):
    2    # return 'I got: None'
    3    # return 'I got: False'
    4    return 'I got: {the_input}'
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: {the_input}'
                        == 'I got: False'
    
  • I change the return statement to an f-string

    1def text(the_input):
    2    # return 'I got: None'
    3    # return 'I got: False'
    4    # return 'I got: {the_input}'
    5    return f'I got: {the_input}'
    6
    7
    8def test_passing_none():
    

    the test passes because Python uses the string representation of the object in the curly braces { }

    text(None) -> 'I got: None'
    └── def text(the_input):
        ├── the_input = None
        └── return f'I got: {the_input}'
            return  'I got:  None      '
    
    text(False) -> 'I got: False'
    └── def text(the_input):
        ├── the_input = False
        └── return f'I got: {the_input}'
            return  'I got:  False     '
    

REFACTOR: make it better


  • I remove the commented lines from the text function

    1def text(the_input):
    2    return f'I got: {the_input}'
    3
    4
    5def test_passing_none():
    
  • I add an assertion for True (the other boolean) to test_passing_booleans

     9def test_passing_booleans():
    10    assert text(False) == 'I got: False'
    11    assert text(True) == 'I got: "True"'
    12
    13
    14# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert 'I got: True'
                == 'I got: "True"
    
  • I remove the quotes around True in my expectation

     9def test_passing_booleans():
    10    assert text(False) == 'I got: False'
    11    # assert text(True) == 'I got: "True"'
    12    assert text(True) == 'I got: True'
    13
    14
    15# Exceptions seen
    

    the test passes because Python uses the string representation of the object in the curly braces { }

    text(True) -> 'I got: True'
    └── def text(the_input):
        ├── the_input = True
        └── return f'I got: {the_input}'
            return  'I got:  True      '
    
  • I add the assert_equal function

    1def assert_equal(a, b):
    2    assert a == b
    3
    4
    5def text(the_input):
    
  • I use the assert_equal function in test_passing_none

     9def test_passing_none():
    10    # assert text(None) == 'I got: None'
    11    assert_equal(text(None), 'I got: BOOM!!!')
    12
    13
    14def test_passing_booleans():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: None' == 'I got: BOOM!!!'
    
  • I change the expectation of the assertion to match reality

     9def test_passing_none():
    10    # assert text(None) == 'I got: None'
    11    # assert_equal(text(None), 'I got: BOOM!!!')
    12    assert_equal(text(None), 'I got: None')
    13
    14
    15def test_passing_booleans():
    

    the test passes.

  • I remove the commented lines from test_passing_none

     9def test_passing_none():
    10    assert_equal(text(None), 'I got: None')
    11
    12
    13def test_passing_booleans():
    
  • I use the assert_equal function in test_passing_booleans

    13def test_passing_booleans():
    14    # assert text(False) == 'I got: False'
    15    # assert text(True) == 'I got: "True"'
    16    # assert text(True) == 'I got: True'
    17    assert_equal(text(False), 'I got: False')
    18    assert_equal(text(True), 'I got: True')
    19
    20
    21# Exceptions seen
    

    the test is still green.

  • I remove the commented lines from test_passing_booleans

    13def test_passing_booleans():
    14    assert_equal(text(False), 'I got: False')
    15    assert_equal(text(True), 'I got: True')
    16
    17
    18# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_passing_booleans'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass booleans as input to a function.


test_passing_an_integer

Can I pass an integer (a whole number without decimals) as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for an integer to test_telephone.py

    13def test_passing_booleans():
    14    assert_equal(text(False), 'I got: False')
    15    assert_equal(text(True), 'I got: True')
    16
    17
    18def test_passing_an_integer():
    19    assert_equal(text(1234), 'I got: "1234"')
    20
    21
    22# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert 'I got: 1234'
                == 'I got: "1234"'
    

GREEN: make it pass


I remove the quotes around the integer in my expectation

18def test_passing_an_integer():
19    # assert_equal(text(1234), 'I got: "1234"')
20    assert_equal(text(1234), 'I got: 1234')
21
22
23# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text(1234) -> 'I got: 1234'
└── def text(the_input):
    ├── the_input = 1234
    └── return f'I got: {the_input}'
        return  'I got:  1234      '

REFACTOR: make it better


  • I add a variable for 1234

    18def test_passing_an_integer():
    19    an_integer = 1234
    20    # assert_equal(text(1234), 'I got: "1234"')
    21    assert_equal(text(1234), 'I got: 1234')
    22
    23
    24# Exceptions seen
    
  • I use the variable to remove repetition of 1234

    18def test_passing_an_integer():
    19    an_integer = 1234
    20    # assert_equal(text(1234), 'I got: "1234"')
    21    # assert_equal(text(1234), 'I got: 1234')
    22    assert_equal(text(an_integer), 'I got: an_integer')
    23
    24
    25# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: 1234'
                        == 'I got: an_integer'
    
  • I change my expectation to an f-string

    18def test_passing_an_integer():
    19    an_integer = 1234
    20    # assert_equal(text(1234), 'I got: "1234"')
    21    # assert_equal(text(1234), 'I got: 1234')
    22    # assert_equal(text(an_integer), 'I got: an_integer')
    23    assert_equal(text(an_integer), f'I got: {an_integer}')
    24
    25
    26# Exceptions seen
    

    the test passes

  • I remove the commented lines from test_passing_an_integer

    18def test_passing_an_integer():
    19    an_integer = 1234
    20    assert_equal(text(an_integer), f'I got: {an_integer}')
    21
    22
    23# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_passing_an_integer'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass an integer as input to a function.


test_passing_a_float

Can I pass a float (binary floating point decimal number) as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a float (binary floating point decimal numbers) to test_telephone.py

    18def test_passing_an_integer():
    19    an_integer = 1234
    20    assert_equal(text(an_integer), f'I got: {an_integer}')
    21
    22
    23def test_passing_a_float():
    24    assert_equal(text(5.678), 'I got: "5.678"')
    25
    26
    27# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert 'I got: 5.678'
                == 'I got: "5.678"'
    

GREEN: make it pass


I remove the quotes around the float in my expectation

23def test_passing_a_float():
24    # assert_equal(text(5.678), 'I got: "5.678"')
25    assert_equal(text(5.678), 'I got: 5.678')
26
27
28# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text(5.678) -> 'I got: 5.678'
└── def text(the_input):
    ├── the_input = 5.678
    └── return f'I got: {the_input}'
        return  'I got:  5.678     '

REFACTOR: make it better


  • I add a variable for 5.678

    23def test_passing_a_float():
    24    a_float = 5.678
    25    # assert_equal(text(5.678), 'I got: "5.678"')
    26    assert_equal(text(5.678), 'I got: 5.678')
    27
    28
    29# Exceptions seen
    
  • I use the variable to remove repetition of 5.678

    23def test_passing_a_float():
    24    a_float = 5.678
    25    # assert_equal(text(5.678), 'I got: "5.678"')
    26    # assert_equal(text(5.678), 'I got: 5.678')
    27    assert_equal(text(a_float), 'I got: a_float')
    28
    29
    30# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: 5.678'
                        == 'I got: a_float'
    
  • I change my expectation to an f-string

    23def test_passing_a_float():
    24    a_float = 5.678
    25    # assert_equal(text(5.678), 'I got: "5.678"')
    26    # assert_equal(text(5.678), 'I got: 5.678')
    27    # assert_equal(text(a_float), 'I got: a_float')
    28    assert_equal(text(a_float), f'I got: {a_float}')
    29
    30
    31# Exceptions seen
    

    the test passes.

  • I remove the commented lines from test_passing_a_float

    23def test_passing_a_float():
    24    a_float = 5.678
    25    assert_equal(text(a_float), f'I got: {a_float}')
    26
    27
    28# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_passing_a_float'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a float as input to a function.


test_passing_a_string

Can I pass a string as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a string (anything in quotes) to test_telephone.py

    23def test_passing_a_float():
    24    a_float = 5.678
    25    assert_equal(text(a_float), f'I got: {a_float}')
    26
    27
    28def test_passing_a_string():
    29    assert_equal(text('hello'), 'I got: hi')
    30
    31
    32# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: hello'
                        == 'I got: hi'
    

GREEN: make it pass


I change my expectation to match reality

28def test_passing_a_string():
29    # assert_equal(text('hello'), 'I got: hi')
30    assert_equal(text('hello'), 'I got: hello')
31
32
33# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text('hello') -> 'I got: hello'
└── def text(the_input):
    ├── the_input = 'hello'
    └── return f'I got: {the_input}'
        return  'I got:  hello     '

REFACTOR: make it better


  • I add a variable for 'hello'

    28def test_passing_a_string():
    29    a_string = 'hello'
    30    # assert_equal(text('hello'), 'I got: hi')
    31    assert_equal(text('hello'), 'I got: hello')
    32
    33
    34# Exceptions seen
    
  • I use the variable to remove repetition of 'hello'

    28def test_passing_a_string():
    29    a_string = 'hello'
    30    # assert_equal(text('hello'), 'I got: hi')
    31    # assert_equal(text('hello'), 'I got: hello')
    32    assert_equal(text(a_string), 'I got: a_string')
    33
    34
    35# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert 'I got: hello'
                        == 'I got: a_string'
    
  • I change my expectation to an f-string

    28def test_passing_a_string():
    29    a_string = 'hello'
    30    # assert_equal(text('hello'), 'I got: hi')
    31    # assert_equal(text('hello'), 'I got: hello')
    32    # assert_equal(text(a_string), 'I got: a_string')
    33    assert_equal(text(a_string), f'I got: {a_string}')
    34
    35
    36# Exceptions seen
    

    the test passes.

  • I remove the commented lines from test_passing_a_string

    28def test_passing_a_string():
    29    a_string = 'hello'
    30    assert_equal(text(a_string), f'I got: {a_string}')
    31
    32
    33# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_passing_a_string'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a string as input to a function.


test_passing_a_tuple

Can I pass a tuple (anything in parentheses ( ) separated by a comma) as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a tuple to test_telephone.py

    28def test_passing_a_string():
    29    a_string = 'hello'
    30    assert_equal(text('hello'), f'I got: {a_string}')
    31
    32
    33def test_passing_a_tuple():
    34    assert_equal(
    35        text((0, 1, 2, 'n')),
    36        "I got: (0, 1, 2, n)"
    37    )
    38
    39
    40# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: (0, 1, 2, 'n')"
                == 'I got: (0, 1, 2, n)'
    

GREEN: make it pass


I change the tuple in my expectation to match reality

33def test_passing_a_tuple():
34    assert_equal(
35        text((0, 1, 2, 'n')),
36        # "I got: (0, 1, 2, n)"
37        "I got: (0, 1, 2, 'n')"
38    )
39
40
41# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text((0, 1, 2, 'n')) -> "I got: (0, 1, 2, 'n')"
└── def text(the_input):
    ├── the_input = (0, 1, 2, 'n')
    └── return f'I got: {  the_input  }'
        return  "I got:  (0, 1, 2, 'n')"

REFACTOR: make it better


  • I add a variable for (0, 1, 2, 'n')

    33def test_passing_a_tuple():
    34    a_tuple = (0, 1, 2, 'n')
    35    assert_equal(
    36        text((0, 1, 2, 'n')),
    37        # "I got: (0, 1, 2, n)"
    38        "I got: (0, 1, 2, 'n')"
    39    )
    40
    41
    42# Exceptions seen
    
  • I use the variable to remove repetition of (0, 1, 2, 'n')

    33def test_passing_a_tuple():
    34    a_tuple = (0, 1, 2, 'n')
    35    assert_equal(
    36        # text((0, 1, 2, 'n')),
    37        # "I got: (0, 1, 2, n)"
    38        # "I got: (0, 1, 2, 'n')"
    39        text(a_tuple), "I got: a_tuple"
    40    )
    41
    42
    43# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    assert "I got: (0, 1, 2, 'n')"
        == 'I got: a_tuple'
    
  • I change my expectation to an f-string

    33def test_passing_a_tuple():
    34    a_tuple = (0, 1, 2, 'n')
    35    assert_equal(
    36        # text((0, 1, 2, 'n')),
    37        # "I got: (0, 1, 2, n)"
    38        # "I got: (0, 1, 2, 'n')"
    39        # text(a_tuple), "I got: a_tuple"
    40        text(a_tuple), f"I got: {a_tuple}"
    41    )
    42
    43
    44# Exceptions seen
    

    the test passes.

  • I remove the commented lines from test_passing_a_tuple

    33def test_passing_a_tuple():
    34    a_tuple = (0, 1, 2, 'n')
    35    assert_equal(text(a_tuple), f"I got: {a_tuple}")
    36
    37
    38# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_passing_a_tuple'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a tuple as input to a function.


test_passing_a_list

Can I pass a list (anything in square brackets [ ]) from a test to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a list to test_telephone.py

    33def test_passing_a_tuple():
    34    a_tuple = (0, 1, 2, 'n')
    35    assert_equal(text(a_tuple), f"I got: {a_tuple}")
    36
    37
    38def test_passing_a_list():
    39    assert_equal(
    40        text([0, 1, 2, 'n']),
    41        'I got: [0, 1, 2, "n"]'
    42    )
    43
    44
    45# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: [0, 1, 2, 'n']"
                == 'I got: [0, 1, 2, "n"]'
    

GREEN: make it pass


I change the list in my expectation to match reality

38def test_passing_a_list():
39    assert_equal(
40        text([0, 1, 2, 'n']),
41        # 'I got: [0, 1, 2, "n"]'
42        "I got: [0, 1, 2, 'n']"
43    )
44
45
46# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text([0, 1, 2, 'n']) -> "I got: [0, 1, 2, 'n']"
└── def text(the_input):
    ├── the_input = [0, 1, 2, 'n']
    └── return f'I got: {  the_input  }'
        return  "I got:  [0, 1, 2, 'n']"

Python changed the double quotes (") in the list to a single quote (').


REFACTOR: make it better


  • I add a variable for [0, 1, 2, 'n']

    38def test_passing_a_list():
    39    a_list = [0, 1, 2, 'n']
    40    assert_equal(
    41        text([0, 1, 2, 'n']),
    42        # 'I got: [0, 1, 2, "n"]'
    43        "I got: [0, 1, 2, 'n']"
    44    )
    45
    46
    47# Exceptions seen
    
  • I use the variable to remove repetition of [0, 1, 2, 'n']

    38def test_passing_a_list():
    39    a_list = [0, 1, 2, 'n']
    40    assert_equal(
    41        # text([0, 1, 2, 'n']),
    42        # 'I got: [0, 1, 2, "n"]'
    43        # "I got: [0, 1, 2, 'n']"
    44        text(a_list), 'I got: a_list'
    45    )
    46
    47
    48# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: [0, 1, 2, 'n']"
                == 'I got: a_list'
    
  • I change my expectation to an f-string

    38def test_passing_a_list():
    39    a_list = [0, 1, 2, 'n']
    40    assert_equal(
    41        # text([0, 1, 2, 'n']),
    42        # 'I got: [0, 1, 2, "n"]'
    43        # "I got: [0, 1, 2, 'n']"
    44        # text(a_list), 'I got: a_list'
    45        text(a_list), f'I got: {a_list}'
    46    )
    47
    48
    49# Exceptions seen
    

    the test is still green.

  • I remove the commented lines from test_passing_a_list

    38def test_passing_a_list():
    39    a_list = [0, 1, 2, 'n']
    40    assert_equal(text(a_list), f'I got: {a_list}')
    41
    42
    43# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_passing_a_list'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a list as input to a function.


test_passing_a_set

Can I pass a set (anything in curly braces { }, not key-value pairs) from a test to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a set to test_telephone.py

    38def test_passing_a_list():
    39    a_list = [0, 1, 2, 'n']
    40    assert_equal(text(a_list), f'I got: {a_list}')
    41
    42
    43def test_passing_a_set():
    44    assert_equal(
    45        text({0, 1, 2, 'n'}),
    46        'I got: {0, 1, 2, "n"}'
    47    )
    48
    49
    50# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: {0, 1, 2, 'n'}"
                == 'I got: {0, 1, 2, "n"}'
    

GREEN: make it pass


  • I change the set in my expectation to match reality

    43def test_passing_a_set():
    44    assert_equal(
    45        text({0, 1, 2, 'n'}),
    46        # 'I got: {0, 1, 2, "n"}'
    47        "I got: {0, 1, 2, 'n'}"
    48    )
    49
    50
    51# Exceptions seen
    
  • I use ctrl/command+s (Windows & Linux/MacOS) to run the test a few times

    • if the result of text({0, 1, 2, 'n'}) is equal to "I got: {0, 1, 2, 'n'}" the test passes because Python uses the string representation of the object in the curly braces { }

      text({0, 1, 2, 'n'}) -> "I got: {0, 1, 2, 'n'}"
      └── def text(the_input):
          ├── the_input = {0, 1, 2, 'n'}
          └── return f'I got: {  the_input  }'
              return  'I got:  {0, 1, 2, 'n'}'
      
    • if the result of text({0, 1, 2, 'n'}) is NOT equal to "I got: {0, 1, 2, 'n'}", the terminal shows AssertionError, for example

      E       assert "I got: {0, 'n', 2, 1}"
                  == "I got: {0, 1, 2, 'n'}"
      

    Python cannot guarantee the order of the things in the set and the order matters for the assertion that is comparing the strings because

    • these two are the same set

      {0, 'n', 2, 1} == {0, 1, 2, 'n'}
      
    • these two are not the same string

      "{0, 'n', 2, 1}" != "{0, 1, 2, 'n'}"
      
  • I add a variable for {0, 1, 2, 'n'}

    43def test_passing_a_set():
    44    a_set = {0, 1, 2, 'n'}
    45    assert_equal(
    46        text({0, 1, 2, 'n'}),
    47        # 'I got: {0, 1, 2, "n"}'
    48        "I got: {0, 1, 2, 'n'}"
    49    )
    50
    51
    52# Exceptions seen
    
  • I use the variable to remove repetition of {0, 1, 2, 'n'}

    43def test_passing_a_set():
    44    a_set = {0, 1, 2, 'n'}
    45    assert_equal(
    46        # text({0, 1, 2, 'n'}),
    47        # 'I got: {0, 1, 2, "n"}'
    48        # "I got: {0, 1, 2, 'n'}"
    49        text(a_set), 'I got: a_set'
    50    )
    51
    52
    53# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: {0, 1, 2, 'n'}"
                == 'I got: a_set'
    
  • I change my expectation to an f-string

    43def test_passing_a_set():
    44    a_set = {0, 1, 2, 'n'}
    45    assert_equal(
    46        # text({0, 1, 2, 'n'}),
    47        # 'I got: {0, 1, 2, "n"}'
    48        # "I got: {0, 1, 2, 'n'}"
    49        # text(a_set), 'I got: a_set'
    50        text(a_set), f'I got: {a_set}'
    51    )
    52
    53
    54# Exceptions seen
    
    • I use ctrl/command+s (Windows & Linux/MacOS) to run the test a few times and the test stays green with no random failures because Python uses the string representation of the object in the curly braces { }.

    • It can guarantee the order when I use a variable and the f-string to refer to the same exact set.


REFACTOR: make it better


  • I remove the commented lines from test_passing_a_set

    43def test_passing_a_set():
    44    a_set = {0, 1, 2, 'n'}
    45    assert_equal(text(a_set), f'I got: {a_set}')
    46
    47
    48# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_passing_a_set'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a set as input to a function.


test_passing_a_dictionary

Can I pass a dictionary (any key-value pairs in curly braces { } separated by commas) as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for a dictionary to test_telephone.py

    43def test_passing_a_set():
    44    a_set = {0, 1, 2, 'n'}
    45    assert_equal(text(a_set), f'I got: {a_set}')
    46
    47
    48def test_passing_a_dictionary():
    49    reality = text({
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    })
    53    my_expectation = (
    54        "I got: "
    55        "{key0: value0, keyN: [0, 1, 2, n]}"
    56    )
    57    assert_equal(reality, my_expectation)
    58
    59
    60# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: {'key..., 1, 2, 'n']}"
                == 'I got: {key0...[0, 1, 2, n]}'
    

    I want more detail in my error messages.


GREEN: make it pass


I change my_expectation to match reality

48def test_passing_a_dictionary():
49    reality = text({
50        'key0': 'value0',
51        'keyN': [0, 1, 2, 'n'],
52    })
53    my_expectation = (
54        "I got: "
55        # "{key0: value0, keyN: [0, 1, 2, n]}"
56        "{'key0': 'value0', 'keyN': [0, 1, 2, 'n']}"
57    )
58    assert_equal(reality, my_expectation)
59
60
61# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text({
    'key0': 'value0', 'keyN': [0, 1, 2, 'n'],}
) -> ("I got: {'key0': 'value0',"
│              'keyN': [0, 1, 2, 'n']}")
└── def text(the_input):
    ├── the_input = {
    │       'key0': 'value0', 'keyN': [0, 1, 2, 'n']
    │   }
    └── return f'I got: {       the_input        }'
        return ("I got: {'key0': 'value0',"
                         'keyN': [0, 1, 2, 'n']}")

REFACTOR: make it better


  • I add a variable for {'key0': 'value0', 'keyN': [0, 1, 2, 'n'],}

    48def test_passing_a_dictionary():
    49    a_dictionary = {
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    }
    53    reality = text({
    54        'key0': 'value0',
    55        'keyN': [0, 1, 2, 'n'],
    56    })
    
  • I use the variable to remove repetition of {'key0': 'value0', 'keyN': [0, 1, 2, 'n'],}

    48def test_passing_a_dictionary():
    49    a_dictionary = {
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    }
    53    # reality = text({
    54    #     'key0': 'value0',
    55    #     'keyN': [0, 1, 2, 'n'],
    56    # })
    57    # my_expectation = (
    58    #     "I got: "
    59    #     # "{key0: value0, keyN: [0, 1, 2, n]}"
    60    #     "{'key0': 'value0', 'keyN': [0, 1, 2, 'n']}"
    61    # )
    62    reality = text(a_dictionary)
    63    my_expectation = 'I got: {a_dictionary}'
    64    assert_equal(reality, my_expectation)
    65
    66
    67# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: {'key..., 1, 2, 'n']}"
                == 'I got: {a_dictionary}'
    
  • I change my_expectation to an f-string

    48def test_passing_a_dictionary():
    49    a_dictionary = {
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    }
    53    # reality = text({
    54    #     'key0': 'value0',
    55    #     'keyN': [0, 1, 2, 'n'],
    56    # })
    57    # my_expectation = (
    58    #     "I got: "
    59    #     # "{key0: value0, keyN: [0, 1, 2, n]}"
    60    #     "{'key0': 'value0', 'keyN': [0, 1, 2, 'n']}"
    61    # )
    62    reality = text(a_dictionary)
    63    # my_expectation = 'I got: {a_dictionary}'
    64    my_expectation = f'I got: {a_dictionary}'
    65    assert_equal(reality, my_expectation)
    66
    67
    68# Exceptions seen
    

    the test passes.

  • I remove the commented lines from test_passing_a_dictionary

    48def test_passing_a_dictionary():
    49    a_dictionary = {
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    }
    53    reality = text(a_dictionary)
    54    my_expectation = f'I got: {a_dictionary}'
    55    assert_equal(reality, my_expectation)
    56
    57
    58# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_passing_a_dictionary'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass a dictionary as input to a function.


test_passing_a_class

Can I pass any object as input to a function?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a failing test to see what happens when I pass a class from a test to the text function, to test_telephone.py

    48def test_passing_a_dictionary():
    49    a_dictionary = {
    50        'key0': 'value0',
    51        'keyN': [0, 1, 2, 'n'],
    52    }
    53    reality = text(a_dictionary)
    54    my_expectation = f'I got: {a_dictionary}'
    55    assert_equal(reality, my_expectation)
    56
    57
    58def test_passing_a_class():
    59    assert_equal(text(object), 'I got: object')
    60
    61
    62# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'object'>"
                == 'I got: object'
    

    object is the mother class that all Python objects come from because everything in Python is an object.


GREEN: make it pass


I change my expectation to match reality

58def test_passing_a_class():
59    # assert_equal(text(object), 'I got: object')
60    assert_equal(
61        text(object), "I got: <class 'object'>"
62    )
63
64
65# Exceptions seen

the test passes because Python uses the string representation of the object in the curly braces { }

text(object) -> "I got: <class 'object'>"
└── def text(the_input):
    ├── the_input = object
    └── return f'I got: {   the_input   }'
        return  "I got:  <class 'object'>"

REFACTOR: make it better


  • I add an assertion for bool (the class for booleans)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    assert_equal(text(bool), 'I got: bool')
    64
    65
    66# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'bool'>"
                == 'I got: bool'
    
  • I change my expectation to match reality for bool

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65
    66
    67# Exceptions seen
    

    the test passes.

  • I add an assertion for int (the object for whole numbers without decimals)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    assert_equal(text(int), 'I got: int')
    66
    67
    68# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'int'>"
                == 'I got: int'
    
  • I change my expectation to match reality for int

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67
    68
    69# Exceptions seen
    

    the test passes.

  • I add an assertion for float (the object for binary floating point decimal numbers)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    assert_equal(text(float), 'I got: float')
    68
    69
    70# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'float'>"
                == 'I got: float'
    
  • I change my expectation to match reality for float

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69
    70
    71# Exceptions seen
    

    the test passes.

  • I add an assertion for str (the object for anything in quotes)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    assert_equal(text(str), 'I got: str')
    70
    71
    72# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'str'>"
                == 'I got: str'
    
  • I change my expectation to match reality for str

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71
    72
    73# Exceptions seen
    

    the test passes.

  • I add an assertion for tuple (the object for anything in parentheses ( ) separated by a comma)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    assert_equal(text(tuple), 'I got: tuple')
    72
    73
    74# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'tuple'>"
                == 'I got: tuple'
    
  • I change my expectation to match reality for tuple

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73
    74
    75# Exceptions seen
    

    the test passes.

  • I add an assertion for list (the object for anything in square brackets ‘[ ]’)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    assert_equal(text(list), 'I got: list')
    74
    75
    76# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'list'>"
                == 'I got: list'
    
  • I change my expectation to match reality for list

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    # assert_equal(text(list), 'I got: list')
    74    assert_equal(text(list), "I got: <class 'list'>")
    75
    76
    77# Exceptions seen
    

    the test passes.

  • I add an assertion for set (the object for anything in curly braces { }, not key-value pairs)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    # assert_equal(text(list), 'I got: list')
    74    assert_equal(text(list), "I got: <class 'list'>")
    75    assert_equal(text(set), 'I got: set')
    76
    77
    78# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'set'>"
                == 'I got: set'
    
  • I change my expectation to match reality for set

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    # assert_equal(text(list), 'I got: list')
    74    assert_equal(text(list), "I got: <class 'list'>")
    75    # assert_equal(text(set), 'I got: set')
    76    assert_equal(text(set), "I got: <class 'set'>")
    77
    78
    79# Exceptions seen
    

    the test passes.

  • I add an assertion for dict (the object for key-value pairs in curly braces ‘{ }’ separated by commas)

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    # assert_equal(text(list), 'I got: list')
    74    assert_equal(text(list), "I got: <class 'list'>")
    75    # assert_equal(text(set), 'I got: set')
    76    assert_equal(text(set), "I got: <class 'set'>")
    77    assert_equal(text(dict), 'I got: dict')
    78
    79
    80# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert "I got: <class 'dict'>"
                == 'I got: dict'
    
  • I change my expectation to match reality for dict

    58def test_passing_a_class():
    59    # assert_equal(text(object), 'I got: object')
    60    assert_equal(
    61        text(object), "I got: <class 'object'>"
    62    )
    63    # assert_equal(text(bool), 'I got: bool')
    64    assert_equal(text(bool), "I got: <class 'bool'>")
    65    # assert_equal(text(int), 'I got: int')
    66    assert_equal(text(int), "I got: <class 'int'>")
    67    # assert_equal(text(float), 'I got: float')
    68    assert_equal(text(float), "I got: <class 'float'>")
    69    # assert_equal(text(str), 'I got: str')
    70    assert_equal(text(str), "I got: <class 'str'>")
    71    # assert_equal(text(tuple), 'I got: tuple')
    72    assert_equal(text(tuple), "I got: <class 'tuple'>")
    73    # assert_equal(text(list), 'I got: list')
    74    assert_equal(text(list), "I got: <class 'list'>")
    75    # assert_equal(text(set), 'I got: set')
    76    assert_equal(text(set), "I got: <class 'set'>")
    77    # assert_equal(text(dict), 'I got: dict')
    78    assert_equal(text(dict), "I got: <class 'dict'>")
    79
    80
    81# Exceptions seen
    

    all the tests are passing because Python uses the string representation of the object in the curly braces { }

    text(any object) ->  "I got: <class 'any object'>"
    └── def text(the_input):
        ├── the_input = dict
        └── return f'I got: {     the_input      }'
            return  "I got:  <class 'any object'> "
    
  • I remove the commented lines from test_passing_a_class

    58def test_passing_a_class():
    59    assert_equal(
    60        text(object), "I got: <class 'object'>"
    61    )
    62    assert_equal(text(bool), "I got: <class 'bool'>")
    63    assert_equal(text(int), "I got: <class 'int'>")
    64    assert_equal(text(float), "I got: <class 'float'>")
    65    assert_equal(text(str), "I got: <class 'str'>")
    66    assert_equal(text(tuple), "I got: <class 'tuple'>")
    67    assert_equal(text(list), "I got: <class 'list'>")
    68    assert_equal(text(set), "I got: <class 'set'>")
    69    assert_equal(text(dict), "I got: <class 'dict'>")
    70
    71
    72# Exceptions seen
    73# AssertionError
    74# NameError
    75# TypeError
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_passing_a_class'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can pass any object as input to a function.


close the project

  • I close test_telephone.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of telephone

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

  • I ran tests to see what happens when I pass objects from a test to a program and place them in an f-string which is one way to do string interpolation.

  • I still have the problem where I wrote the assert_equal function in this project, even though I wrote it in previous projects. There has to be a better way where I can write the function once, then use it any time or anywhere I want.


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

would you like to test separating the tests from the solutions?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.