Aug-31-2018, 12:50 AM
So I'm using the book 'Python Crash Course' and working on the chapter dealing with testing your code. My function 'get_formatted_name' is supposed to accept 3 arguments. However 'middle' is supposed to be optional. I thought I did that correctly but when I test the code with unittest I'm getting an error.
Here is get_formatted_name:
Here is get_formatted_name:
def get_formatted_name(first, last, middle=''): """Generate a neatly formatted first and last name""" if middle: full_name = first + ' ' + middle + ' ' + last else: full_name = first + ' ' + last return full_name.title()Here is where I'm testing it:
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """Tests for 'name_function.py'.""" def test_first_last_name(self): """Do names like 'Janis Joplin' work?""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """Do names like 'Wolfgang Amadeus Mozart' work?""" formatted_name = get_formatted_name( 'wolfgang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart') unittest.main()And here is the error I'm getting:
Error:E.
======================================================================
ERROR: test_first_last_middle_name (__main__.NamesTestCase)
Do names like 'Wolfgang Amadeus Mozart' work?
----------------------------------------------------------------------
Traceback (most recent call last):
File "I removed this file path", line 18, in test_first_last_middle_name
'wolfgang', 'mozart', 'amadeus')
TypeError: get_formatted_name() takes 2 positional arguments but 3 were given
----------------------------------------------------------------------
Ran 2 tests in 0.000s
So I see where it says "takes 2 positional arguments but 3 were given". This leads me to believe I didn't successfully make 'middle' optional. But I did it just like the book shows for optional arguments and I'm still getting the error. Any ideas? Thanks in advance!