|
在下是新手,刚刚学习python,使用的ID E是pychram 5.0.4
今天学习使用unittest做测试,非常简单
需要测试的代码如下:
def get_formatted_name (first,last):
"""Generate a neatly formatted full name"""
full_name = first + " " + last
return full_name.title()
测试代码如下:
import unittest
from name_function import get_formatted_name
class NamesTestCase (unittest.TestCase):
def test_first_last_name (self):
formatted_name = get_formatted_name ('janis','joplin')
self.assertEqual (formatted_name, 'Janis Joplin')
unittest.main()
都按照书中的进行copy,但发生以下错误:D:\Python34\python.exe "D:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\utrunner.py" "D:\Python Crash Course\Part 11\test_name_function.py" true
Testing started at 21:49 ...
Traceback (most recent call last):
File "D:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\utrunner.py", line 121, in <module>
modules = [loadSource(a[0])]
File "D:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\utrunner.py", line 41, in loadSource
module = imp.load_source(moduleName, fileName)
File "D:\Python34\lib\imp.py", line 171, in load_source
module = methods.load()
File "<frozen importlib._bootstrap>", line 1220, in load
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "D:\Python Crash Course\Part 11\test_name_function.py", line 9, in <module>
unittest.main()
File "D:\Python34\lib\unittest\main.py", line 92, in __init__
self.parseArgs(argv)
File "D:\Python34\lib\unittest\main.py", line 139, in parseArgs
self.createTests()
File "D:\Python34\lib\unittest\main.py", line 146, in createTests
self.module)
File "D:\Python34\lib\unittest\loader.py", line 146, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "D:\Python34\lib\unittest\loader.py", line 146, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "D:\Python34\lib\unittest\loader.py", line 114, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_name_function'
百思不得其解,希望老司机能帮助一下,谢谢 |
|