test AssertionError with functions


I want to use the assert_equal function and make other functions like it in the AssertionError project.


preview

I have these tests by the end of the chapter

assertion_error/tests/test_assertion_error.py
 1def assert_is_not(x, y):
 2    assert x is not y
 3
 4
 5def assert_is_not_none(x):
 6    assert_is_not(x, None)
 7
 8
 9def assert_is_not_false(x):
10    assert_is_not(x, False)
assertion_error/tests/test_assertion_error.py
13def assert_is_not_true(x):
14    assert_is_not(x, True)
15
16
17def assert_not_equal(x, y):
18    assert x != y
19
20
21def assert_equal(x, y):
22    assert x == y
assertion_error/tests/test_assertion_error.py
25def test_assert_keyword():
26    assert_equal(1+1, 2)
27    assert_equal('1'+'1', '11')
28    assert_equal('I am'+' alive', 'I am alive')
assertion_error/tests/test_assertion_error.py
31def test_assertion_error_w_none():
32    assert None is None
33
34    assert_is_not_none(False)
35    assert_is_not_none(True)
36    assert_is_not_none(0)
37    assert_is_not_none(0.0)
38    assert_is_not_none('')
39    assert_is_not_none(())
40    assert_is_not_none([])
41    assert_is_not_none(set())
42    assert_is_not_none({})
assertion_error/tests/test_assertion_error.py
45def test_assertion_error_w_false():
46    assert False is False
47
48    assert_is_not_false(None)
49    assert_is_not_false(True)
50    assert_is_not_false(0)
51    assert_is_not_false(0.0)
52    assert_is_not_false('')
53    assert_is_not_false(())
54    assert_is_not_false([])
55    assert_is_not_false(set())
56    assert_is_not_false({})
assertion_error/tests/test_assertion_error.py
59def test_assertion_error_w_true():
60    assert True is True
61
62    assert_is_not_true(None)
63    assert_is_not_true(False)
64    assert_is_not_true(0)
65    assert_is_not_true(0.0)
66    assert_is_not_true('')
67    assert_is_not_true(())
68    assert_is_not_true([])
69    assert_is_not_true(set())
70    assert_is_not_true({})
assertion_error/tests/test_assertion_error.py
73def test_assertion_error_w_equality():
74    assert_equal(None, None)
75    assert_equal(False, False)
76    assert_equal(True, True)
77
78    assert_not_equal(False, None)
79    assert_not_equal(False, True)
80    assert_not_equal(True, None)
assertion_error/tests/test_assertion_error.py
83def test_assertion_error_w_is_vs_equal():
84    assert_is_not(0, 0.0)
85    assert_equal(0, 0.0)
86
87
88def will_not_run():
89    # will not run because
90    # the name does not start with test
91    assert False == True
92
93
94def test_failure():
95    # assert False == True
96    assert_not_equal(False, True)
97
98
99# NOTES

open the project


test_assert_keyword with assert_equal

RED: make it fail



GREEN: make it pass


  • I change the expectation from 3 to 2 to match reality for the first assertion in test_assert_keyword

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: assert '11' == '2'
    
    assert_equal('1'+'1', '2') -> None
    └── def assert_equal(x, y):
        ├── x = '1' + '1'
        │     = '11'
        ├── y = '2'
        └── assert x    == y
            assert '11' == '2'
    
  • I change the expectation from '2' to '11' to match reality for the second assertion in test_assert_keyword

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: assert 'I am alive' == 'I am a ghost'
    
    assert_equal('I am'+' alive', 'I am a ghost') -> None
    └── def assert_equal(x, y):
        ├── x = 'I am' + ' alive'
        │     = 'I am alive'
        ├── y = 'I am a ghost'
        └── assert x            == y
            assert 'I am alive' == 'I am a ghost'
    
  • I change the expectation from 'I am a ghost' to 'I am alive' for the third assertion in test_assert_keyword


REFACTOR: make it better


  • I remove the commented lines from test_assert_keyword

  • I add a git commit message in the new terminal

    terminal
    git commit -am \
    'test_assert_keyword with assert_equal'
    

test_assertion_error_w_equality with assert_equal

RED: make it fail



GREEN: make it pass



REFACTOR: make it better


  • I remove the commented lines from test_assertion_error_w_equality since they are now repetitions

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'test_assertion_error_w_equality with assert_equal'
    

test_assertion_error_w_is_vs_equal with assert_equal

RED: make it fail



GREEN: make it pass


I change 1 to 0 in the second assertion of test_assertion_error_w_is_vs_equal

87def test_assertion_error_w_is_vs_equal():
88    assert 0 is not 0.0
89
90    # assert 0 == 0.0
91    # assert_equal(1, 0.0)
92    assert_equal(0, 0.0)
93
94
95def will_not_run():

the test passes.


REFACTOR: make it better



extract assert_not_equal function

The remaining assertions in test_assertion_error_w_equality and assertion in test_failure are the same, they check if something is not equal to something else.

assert something != something_else

I can use a function to assert if two things are NOT equal.


RED: make it fail


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

  • I add a function named assert_not_equal that takes two inputs and asserts that they are NOT equal, in test_assertion_error.py

    1def assert_not_equal(x, y):
    2    assert x != y
    3
    4
    5def assert_equal(x, y):
    
  • I use the new function for the remaining assertions in test_assertion_error_w_equality

    the terminal is my friend, and shows AssertionError

    E       assert None != None
    
    assert_not_equal(None, None) -> None
    └── def assert_not_equal(x, y):
        ├── x = None
        ├── y = None
        └── assert x    != y
            assert None != None
    

GREEN: make it pass



REFACTOR: make it better



extract assert_is_not function

The other assertions in test_assertion_error.py are the same, they check if something is NOT the same object as something else

assert something is not something_else

three of the assertions are the same, they check if something is the same object as something else

assert something is something_else

I can use a function to assert if one object is NOT the same object as another.


RED: make it fail


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

  • I add a function named assert_is_not that takes two inputs and asserts that they are not the same object, in test_assertion_error.py

    1def assert_is_not(x, y):
    2    assert x is not y
    3
    4
    5def assert_not_equal(x, y):
    
  • I use the assert_is_not function for the assertions in test_assertion_error_w_none

    the terminal is my friend, and shows AssertionError

    E       assert None is not None
    
    assert_is_not(None, None) -> None
    └── def assert_is_not(x, y):
        ├── x = None
        ├── y = None
        └── assert x    is not y
            assert None is not None
    

GREEN: make it pass



extract assert_is_not_none function

All the assertions in test_assertion_error_w_none except the first one, use the assert_is_not function to assert that something is NOT the same object as None

assert something is not None

I can use a function to remove repetition of None from the call to the assert_is_not function to assert that something is NOT the same object as None.


RED: make it fail


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

  • I add a function named assert_is_not_none that takes one input and calls the assert_is_not function to assert that the input is NOT the same object as None, in test_assertion_error.py

    1def assert_is_not(x, y):
    2    assert x is not y
    3
    4
    5def assert_is_not_none(x):
    6    assert_is_not(x, None)
    7
    8
    9def assert_not_equal(x, y):
    
  • I use the assert_is_not_none function for the second assertion in test_assertion_error_w_none

    the terminal is my friend, and shows AssertionError

    E       assert None is not None
    
    assert_is_not_none(None) -> None
    └── def assert_is_not_none(x):
        ├── x = None
        └── assert_is_not(x, None)
            └── def assert_is_not(x, y):
                ├── x = None
                ├── y = None
                └── assert x    is not y
                    assert None is not None
    

GREEN: make it pass


I change None to False for the second assertion in test_assertion_error_w_none

23  def test_assertion_error_w_none():
24      assert None is None
25
26      # assert False is not None
27      # assert_is_not(None, None)
28      # assert_is_not(False, None)
29      # assert_is_not_none(None)
30      assert_is_not_none(False)
31
32      # assert True is not None

the test passes.

assert_is_not_none(False) -> None
└── def assert_is_not_none(x):
    ├── x = False
    └── assert_is_not(x, None)
        └── def assert_is_not(x, y):
            ├── x = False
            ├── y = None
            └── assert x     is not y
                assert False is not None

REFACTOR: make it better



extract assert_is_not_false function

The assertions in test_assertion_error_w_false except assert False is False are the same, they check if something is NOT the same object as False

assert something is not False

I can use a function to assert that something is NOT the same object as False.


RED: make it fail



GREEN: make it pass


I change False to None for the first assertion in test_assertion_error_w_false

41def test_assertion_error_w_false():
42    # assert None is not False
43    # assert_is_not_false(False)
44    assert_is_not_false(None)
45
46    assert False is False

the test passes.

assert_is_not_false(None) -> None
└── def assert_is_not_false(x):
    ├── x = None
    └── assert_is_not(x, False)
        └── def assert_is_not(x, y):
            ├── x = None
            ├── y = False
            └── assert x    is not y
                assert None is not False

REFACTOR: make it better



extract assert_is_not_true function

The assertions in test_assertion_error_w_true except assert True is True are the same, they check if something is NOT the same object as True

assert something is not True

I can use a function to assert that something is NOT the same object as True.


RED: make it fail



GREEN: make it pass


I change True to None for the first assertion in test_assertion_error_w_true

59def test_assertion_error_w_true():
60    # assert None is not True
61    # assert_is_not_true(True)
62    assert_is_not_true(None)
63
64    assert False is not True

the test passes.

assert_is_not_true(None) -> None
└── def assert_is_not_true(x):
    ├── x = None
    └── assert_is_not(x, True)
        └── def assert_is_not(x, y):
            ├── x = None
            ├── y = True
            └── assert x    is not y
                assert None is not True

REFACTOR: make it better



close the project

  • I close test_assertion_error.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 assertion_error

    terminal
    cd ..
    

    the terminal is my friend, and shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review


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 using a function to make a string from input?


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.