functions 2
Since I know how to use variables, I can do better with the results of test_why_use_a_function
open the project
I change directory to the
functionsfoldercd functionsthe terminal shows I am in the
functionsfolder.../pumping_python/functions (main)I use
pytest-watcherto run the testsuv run pytest-watcher . --nowthe 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.pyto open it in the editor
a better way to handle the results changing
RED: make it fail
I add 2 variables to test_why_use_a_function
7 def test_why_use_a_function(self): 8 def add_x(x=3, y=0): 9 return x + y 10 11 x = 3 12 y = 0 13 self.assertEqual(add_x(y=0), 3)I use the variables in the assertion
11 x = 3 12 y = 0 13 # self.assertEqual(add_x(y=0), 3) 14 self.assertEqual(add_x(x, y), x+x) 15 self.assertEqual(add_x(y=1), 4)the terminal shows AssertionError
AssertionError: 3 != 6
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 != 6I 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 != 4I 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
xandyare both3on 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 != 8I 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 != 8I 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 != 9I 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 != 14I 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 != 6I 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 Symbolfeature to change the name of theadd_xfunction toadd7 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
4to a number. I changex7 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
5to a number. I changex7 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
6to a number. I changex7 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
close the project
I close
test_functions.pyandfunctions.pyin the editorI 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
functionscd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I ran tests to show that I can make functions with
the def keyword
as a reminder
code from the 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 give the project a name
I make a Python file to hold the source code in the ‘src’ folder
I install the Python packages listed in the requirements file
I make the test pass
then I start working on the project
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