I tried to make a simple unittest in Python, but I don't know why the test failed. I have made three files:
- name_function.py in which I have a function that receive two parameters (first name, last name) and return the concatenated name.
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
- names.py in which the user is asked to enter the first name and last name or q to quit. After that the function get_formatted_name is called and concatenated name is printed.
from name_function import get_formatted_name
print("\n Enter 'q' at any time to quit.")
while True:
first = input("\n Please give me a first name : ")
if first == 'q':
break
last = input("\n Please give me a second name : ")
if last == 'q':
break
formatted_name = get_formatted_name(first, last)
print("\n\t Neatly formatted name : " + formatted_name + '.')
- test_name_function.py where the function is tested.
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('Clint', 'Eastwood')
self.assertEqual(formatted_name, 'Clint Eastwood')
unittest.main()
- In this window, I run cmd command (see attach Capture_1).
- In cmd I run the command (see attach Capture_2 and Capture_3).
- I don't understand where is my mistake? In Capture_3 see what I obtain when I run the test.
- I use Python 3.7.2 and the IDE Python that I use is PyCharm.