Here are the instructions for the exercise / task I am working on:
Two-fer or 2-fer is short for two for one. One for you and one for me. For this task, given a name, return a string with the message:
One for <name>, one for me.
…where “name” is the given name. However, if the name is missing, return the string:
One for you, one for me.
When the variable name
contains strings such as “Bob” or “Alice”, when the unit test runs, the function I wrote returns:
One for Bob, one for me.
One for Alice, one for me.
Since my script produces this output, I pass 2 of 3 unit tests. They flash green. However the failed unit test shows a traceback in my shell indicating a TypeError: two_fer() missing 1 required positional argument: 'name'
. When I see this Type Error, I figure it is telling me to create a condition that when an argument of None
or an empty string is passed in, the consequent (a string of One for you, one for me.
) should be triggered. I feel that the latest iteration of my script accounts for that condition but evidently I’m still not doing something right.
The class method in the unit test includes an assertion where if no name is given: self.assertEqual(two_fer(), "One for you, one for me.")
. However if I sneakily insert an empty string or None
into that line so it says two_fer(“”)
, then my script passes. But I realize that is kind of cheating.
Could someone shed some more light or meaning onto this TypeError I am encountering?
Below are three code snippets: (a) the latest iteration of my script, (b) the unit test, (c) the pytest traceback in my shell.
Script:
def two_fer(name):
if name:
return f"One for {name}, one for me."
else:
return f"One for you, one for me."
Full unit test:
import unittest
from two_fer import (
two_fer,
)
# Tests adapted from `problem-specifications//canonical-data.json`
class TwoFerTest(unittest.TestCase):
def test_no_name_given(self):
self.assertEqual(two_fer(), "One for you, one for me.")
def test_a_name_given(self):
self.assertEqual(two_fer("Alice"), "One for Alice, one for me.")
def test_another_name_given(self):
self.assertEqual(two_fer("Bob"), "One for Bob, one for me.")
if __name__ == "__main__":
unittest.main()
pytest command traceback:
$ python -m pytest two_fer_test.py
================test session starts ===========
platform linux -- Python 3.9.7, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/gnull/dev/projects/python/2018-and-2020/exercism-v3/python/two-fer
collected 3 items
two_fer_test.py ..F [100%]
==================FAILURES ===================
________ TwoFerTest.test_no_name_given __________
self = <two_fer_test.TwoFerTest testMethod=test_no_name_given>
def test_no_name_given(self):
> self.assertEqual(two_fer(), "One for you, one for me.")
E TypeError: two_fer() missing 1 required positional argument: 'name'
two_fer_test.py:12: TypeError
==================== short test summary info ===================
FAILED two_fer_test.py::TwoFerTest::test_no_name_given - TypeError: two_fer() missing 1 required positional argument: 'name'