test_functions¶
test_functions_w_pass¶
red: make it fail¶
I open a terminal to run makePythonTdd.sh with
functions
as the name of the project./makePythonTdd.sh functions
on Windows without Windows Subsystem Linux use makePythonTdd.ps1
./makePythonTdd.ps1 functions
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_functions.py:7: AssertionError
I hold
ctrl
(windows/linux) oroption
(mac) on the keyboard and use the mouse to click ontests/test_functions.py:7
to open it in the editorthen change
True
toFalse
and change
test_failure
import unittest
import functions
class TestFunctions(unittest.TestCase):
def test_functions_w_pass(self):
self.assertIsNone(functions.function_w_pass())
the terminal shows ModuleNotFoundError , and I add it to the list of Exceptions encountered
# Exceptions Encountered
# AssertionError
# ModuleNotFoundError
green: make it pass¶
I make a file called
functions.py
in the project folder and the terminal shows AttributeError, which I add to the list of Exceptions encountered# Exceptions Encountered # AssertionError # ModuleNotFoundError # AttributeError
I add a function definition to
functions.py
def function_w_pass(): pass
and we have a passing test
the test checks if the value of the call to
functions.function_w_pass
is Nonethe function definition simply says pass yet the test passes
pass is a placeholder keyword which allows the function definition to follow Python syntax rules
the test passes because in Python all functions return None by default, like the function has an invisible line that says
return None
test_functions_w_return¶
red: make it fail¶
I add a new failing test to TestFunctions
in test_functions.py
to check that functions always return None
def test_functions_w_return(self):
self.assertIsNone(functions.function_w_return())
the terminal shows AttributeError
green: make it fail¶
I add a new function to functions.py
to make the test pass, this time with a return
statement instead of pass
def function_w_return(self):
return
the terminal shows this test also passes
I defined 2 functions with different statements in their body but they both return the same result, because “in Python all functions return None by default, like the function has an invisible line that says return None
”
test_functions_w_return_none¶
red: make it fail¶
I add one more test to the TestFunctions
class in test_functions.py
to help drive home the point
def test_functions_w_return_none(self):
self.assertIsNone(
functions.function_w_return_none()
)
the terminal shows AttributeError
green: make it pass¶
from the Zen of Python: Explicit is better than implicit.
I add a function definition to functions.py
this time with an explicit return
statement showing the value returned
def function_w_return_none():
return None
and the terminal shows passing tests.
review¶
The 3 ways I have defined functions so far have the exact same outcome, they all return None
. If Explicit is better than implicit.
then I prefer to use return None
telling anyone who reads the code exactly what the function returns.
Here is what I know so far from the tests
Would you like to test singleton functions?