functions 3

Since I know how to use for loops, I can do better with the assertions of test_why_use_a_function


open the project

  • I change directory to the functions folder

    cd functions
    

    the terminal shows I am in the functions folder

    .../pumping_python/functions (main)
    
  • I use pytest-watcher to run the tests

    uv run pytest-watcher . --now
    

    the terminal shows

    rootdir: .../pumping_python/functions
    configfile: pyproject.toml
    collected 12 items
    
    tests/test_functions.py ............                          [100%]
    
    ======================= 12 passed in X.YZs =========================
    
  • I hold ctrl on the keyboard and click on tests/test_functions.py to open it in the editor


a better way to test why use a function


RED: make it fail



GREEN: make it pass


I change the expectation

14          x = 3
15          y = 0
16          # self.assertEqual(add_x(y=0), 3)
17          self.assertEqual(add_x(x, y), x+y)
18          self.assertEqual(add_x(y=1), 4)

the test passes


REFACTOR: make it better


  • I remove the comment then do the same thing for the next assertion

    11        x = 3
    12        y = 0
    13        self.assertEqual(add_x(x, y), x+y)
    14        y = 1
    15        # self.assertEqual(add_x(y=1), 4)
    16        self.assertEqual(add_x(x, y), x+x)
    17        self.assertEqual(add_x(y=2), 5)
    

    the terminal shows AssertionError

    AssertionError: 4 != 6
    
  • I change the expectation

    14        y = 1
    15        # self.assertEqual(add_x(y=1), 4)
    16        self.assertEqual(add_x(x, y), x+y)
    17        self.assertEqual(add_x(y=2), 5)
    

    the test passes. Wait a minute, self.assertEqual(add_x(x, y), x+y) is also a repetition!

  • I remove the comment then add a variable for the next assertion

    14        y = 1
    15        self.assertEqual(add_x(x, y), x+y)
    16        y = 2
    17        # self.assertEqual(add_x(y=2), 5)
    18        self.assertEqual(add_x(x, y), y+y)
    19        self.assertEqual(add_x(y=3), 6)
    

    the terminal shows AssertionError

    AssertionError: 5 != 4
    
  • I change the expectation to the right calculation

    16        y = 2
    17        # self.assertEqual(add_x(y=2), 5)
    18        self.assertEqual(add_x(x, y), x+y)
    19        self.assertEqual(add_x(y=3), 6)
    

    the test passes, this is the same as the add function from how to make a calculator

  • I remove the comment, then add a variable for the next assertion

    16        y = 2
    17        self.assertEqual(add_x(x, y), x+y)
    18        y = 3
    19        # self.assertEqual(add_x(y=3), 6)
    20        self.assertEqual(add_x(x, y), x+y)
    21        self.assertEqual(add_x(y=4), 7)
    

    the test is still green because x and y are both 3

  • on to the next one

    18        y = 3
    19        self.assertEqual(add_x(x, y), x+y)
    20        y = 4
    21        # self.assertEqual(add_x(y=4), 7)
    22        self.assertEqual(add_x(x, y), y+y)
    23        self.assertEqual(add_x(y=5), 8)
    

    the terminal shows AssertionError

    AssertionError: 7 != 8
    
  • I change the expectation

    20        y = 4
    21        # self.assertEqual(add_x(y=4), 7)
    22        self.assertEqual(add_x(x, y), x+y)
    23        self.assertEqual(add_x(y=5), 8)
    

    the test passes

  • I remove the comment, then add a variable

    20        y = 4
    21        self.assertEqual(add_x(x, y), x+y)
    22        y = 5
    23        # self.assertEqual(add_x(y=5), 8)
    24        self.assertEqual(add_x(x, x), x+y)
    25        self.assertEqual(add_x(y=6), 9)
    

    the terminal shows AssertionError

    AssertionError: 6 != 8
    
  • I change the call

    22        y = 5
    23        # self.assertEqual(add_x(y=5), 8)
    24        self.assertEqual(add_x(x, y), x+y)
    25        self.assertEqual(add_x(y=6), 9)
    

    the test passes

  • I remove the comment then add a variable

    22        y = 5
    23        self.assertEqual(add_x(x, y), x+y)
    24        y = 6
    25        # self.assertEqual(add_x(y=6), 9)
    26        self.assertEqual(add_x(y, y), x+y)
    27        self.assertEqual(add_x(y=7), 10)
    

    the terminal shows AssertionError

    AssertionError: 12 != 9
    
  • I change the call

    24        y = 6
    25        # self.assertEqual(add_x(y=6), 9)
    26        self.assertEqual(add_x(x, y), x+y)
    27        self.assertEqual(add_x(y=7), 10)
    

    the test passes

  • I remove the comment then add a variable

    24        y = 6
    25        self.assertEqual(add_x(x, y), x+y)
    26        y = 7
    27        # self.assertEqual(add_x(y=7), 10)
    28        self.assertEqual(add_x(x, y), y+y)
    29        self.assertEqual(add_x(y=8), 11)
    

    the terminal shows AssertionError

    AssertionError: 10 != 14
    
  • I change the expectation

    26        y = 7
    27        # self.assertEqual(add_x(y=7), 10)
    28        self.assertEqual(add_x(x, y), x+y)
    29        self.assertEqual(add_x(y=8), 11)
    

    the test passes

  • I remove the comment then add one more variable

    26        y = 7
    27        self.assertEqual(add_x(x, y), x+y)
    28        y = 8
    29        # self.assertEqual(add_x(y=8), 11)
    30        self.assertEqual(add_x(x, y), x+x)
    

    the terminal shows AssertionError

    AssertionError: 11 != 6
    
  • I change the expectation

    26        y = 8
    27        # self.assertEqual(add_x(y=8), 11)
    28        self.assertEqual(add_x(x, y), x+y)
    

    the test passes

  • I remove the comment then use the Rename Symbol feature to change the name of the add_x function to add

     7    def test_why_use_a_function(self):
     8        def add(x=3, y=0):
     9            return x + y
    10
    11        x = 4
    12        y = 0
    13        self.assertEqual(add(x, y), x+y)
    14        y = 1
    15        self.assertEqual(add(x, y), x+y)
    16        y = 2
    17        self.assertEqual(add(x, y), x+y)
    18        y = 3
    19        self.assertEqual(add(x, y), x+y)
    20        y = 4
    21        self.assertEqual(add(x, y), x+y)
    22        y = 5
    23        self.assertEqual(add(x, y), x+y)
    24        y = 6
    25        self.assertEqual(add(x, y), x+y)
    26        y = 7
    27        self.assertEqual(add(x, y), x+y)
    28        y = 8
    29        self.assertEqual(add(x, y), x+y)
    30
    31    def test_making_a_function_w_pass(self):
    
  • I only have to make a change in one place if I want to test what happens when I add 4 to a number. I change x

     7    def test_why_use_a_function(self):
     8        def add(x=3, y=0):
     9            return x + y
    10
    11        x = 4
    12        y = 0
    13        self.assertEqual(add(x, y), x+y)
    

    the test is still green

  • I only have to make a change in one place if I want to test what happens when I add 5 to a number. I change x

     7    def test_why_use_a_function(self):
     8        def add(x=3, y=0):
     9            return x + y
    10
    11        x = 5
    12        y = 0
    13        self.assertEqual(add_x(x, y), x+y)
    

    still green.

  • I only have to make a change in one place if I want to test what happens when I add 6 to a number. I change x

     7    def test_why_use_a_function(self):
     8        def add(x=3, y=0):
     9            return x + y
    10
    11        x = 6
    12        y = 0
    13        self.assertEqual(add_x(x, y), x+y)
    

    green. Much better than what I had before, the test passes for any number I try, but there has to be a better way that does not need all these lines of code

I can use a variable to remove duplication


close the project

  • I close test_functions.py and functions.py in the editor

  • I click in the terminal and use q on the keyboard to leave the tests and the terminal goes back to the command line

  • I change directory to the parent of functions

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory


review

I ran tests to show that I can make functions with

as a reminder

How many questions can you answer about functions?


code from the chapter

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


what is next?

you know

There is a problem, I have done these same steps for each of the chapters up till now

I think I know how to make a Python Test Driven Development environment. I am going to write a program that will do all the steps for making a project for me, so I never have to do those steps again.

Would you like to know how to make a Python Test Driven Development environment automatically?


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