0

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.

Capture_1 Capture_2 Capture_3

3 Answers 3

1

Your code looks good to me. I ran it on my machine and it works great. The only thing I noticed is python -m unittest does not find your test. A quick workaround would be to add the following at the end of your unittest file.

if __name__ == '__main__':
    unittest.main()

Then, you can run it with the following command. python test_name_function.py

Sign up to request clarification or add additional context in comments.

1 Comment

I think the problem is that without the guard, unittest.main() is being called again when the test file is imported and is doing a sys.exit(1) internally which results in the error.
0

Since you are asking about PyCharm specifically, here's all you need for that:

  • remove the last line from your unit test file: unittest.main()
  • or do what @Zykerd suggested and turn it into

This:

if __name__ == '__main__':
    unittest.main()
  • rightclick the test in PyCharm and select Run Unittests in test_name_function

Both work (i.e. without the call to main, or with the call behind the main name check.

2 Comments

Thank you Grismar.
You're welcome - if the answer answered your question, please accept it with the checkmark so the question is marked answered.
0

This is because it is assumed that you are running on the interactive interpreter and at the point of the exception it fails with a SystemExit. However, Like mentioned in the comment from @Zykerd the

if __name__ == '__main__':
    unittest.main()

above would solve this problem by assuming that the script is being run from the command line instead of interactive interpreter.

Cheers!

Please check here for further explanation : Tests succeed, still get traceback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.