Home > other >  Python test class error: AttributeError: module '__main__' has no attribute 'test_nam
Python test class error: AttributeError: module '__main__' has no attribute 'test_nam

Time:09-30

Beginners python, write a function of the unit test, then an error is as follows:
EE
======================================================================
ERROR: test_name_function (unittest. Loader. _FailedTest)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
AttributeError: module '__main__' has no attribute 'test_name_function'

======================================================================
ERROR: true (unittest. Loader. _FailedTest)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
AttributeError: module '__main__' has no attribute 'true'

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Name_function. Py:
 def get_formatted_name (first, last, middle=' ') : 
If middle:
Full_name=first + + "" +" "+ middle last;
The else:
Full_name=first + "" + last;
Return full_name. Title ();


Test_name_function. Py
 import unittest 
The from ch02. Owntest. Name_function import get_formatted_name
The class NameTestCase (unittest. TestCase) :
Def test_first_last_name (self) :
Format_name=get_formatted_name (' king 'and' Ivan ')
Self. AssertEqual (format_name, 'King Ivan')

Unittest. The main ()


Run this test_name_function unittest. The main () to quote the above error, who know how to return a responsibility

CodePudding user response:

Need to run the following:
If __name__=="__main__ ':
Unittest. The main ()
Estimation and need a main method in Java

CodePudding user response:

Try using an editor, this example I run in the pycharm cannot pass, but can be in Geany,

CodePudding user response:

And you use python version, 2 words need
If __name__=="__main__ ':
Unittest. The main ()
3 don't need it

CodePudding user response:

This problem is because open the folder, pycharm as a project to open, then perform the unit test script, will be out of the question,
If it is as the file open, just don't open the folder, so there is no any problem to run,
But why will appear this problem, did not find a deeper reason, guess and Pycharm Project internal processing has a certain relationship,
So the solution a few
1. Pycharm single open the file in, don't open the folder;
2. In an ide, such as Notpad++ etc.

CodePudding user response:

I also appear the same problem, I would like to ask the original poster is how to solve this problem, thank you, I am using jupyter notebook the editor

CodePudding user response:

I also met, I use the web version jupyter notebook, code is similar to you, so also wrong, I don't know why

CodePudding user response:

Unittest. Main (argv=[' first - arg - is - ignored '], exit=False) or unittest. Main (argv=[' ignored ', '-v'], exit=False)

To this
  • Related