Python 在PyCharm-atributeerror:module'__主&';没有任何特权

Python 在PyCharm-atributeerror:module'__主&';没有任何特权,python,pycharm,Python,Pycharm,我认为这更像是一个PyCharm问题,而不是unittest问题,但我正在学习一本书中的测试,这个例子写得不好。我正在运行PyCharm 2016.3.2和Python 3.6 import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """Tests for 'name_function.py'.""" def test_first

我认为这更像是一个PyCharm问题,而不是unittest问题,但我正在学习一本书中的测试,这个例子写得不好。我正在运行PyCharm 2016.3.2和Python 3.6

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')

# In book, it's just the unittest.main() , which does not work...
# So in PyCharm __name__ doesn't equal __main__ ... not sure why
# if __name__ == '__main__':
    unittest.main()
以下是name_function.py:

def get_formatted_name(first, last):
    """Generate a neatly formatted full name."""
    full_name = first + " " + last
    return full_name.title()
如果以书面形式运行,则会出现以下错误:

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'

----------------------------------------------------------------------
Ran 2 tests in 0.000s
如果我让“If”语句运行(或者只是删除对unittest.main()的调用),那么它工作正常,根本没有main()调用

这都是在一个
test\u name\u function.py
中完成的。因此,当运行这一个文件(并导入<代码> NAMEXFUNCTION,PY < /代码>)时,PyCharm不认为这一个文件是“代码”?PyCharm在幕后做什么


我对Python和PyCharm还不熟悉,我试图了解结构和环境。非常感谢各位。

你们可以用这个方法来解决这个问题

 suite = unittest.defaultTestLoader.loadTestsFromTestCase(NamesTestCase)
 unittest.TextTestRunner().run(suite)

谢谢你的编辑菲利普