【发布时间】:2015-08-10 17:31:57
【问题描述】:
我正在尝试为我的服务器设置一些测试。
根据朋友的推荐,我想使用 unittest 和 mock。为此,我写了几行,但是当我尝试执行脚本时,我得到了一个 importError。
Traceback (most recent call last):
File "test_creds.py", line 146, in <module>
unittest.main()
AttributeError: 'module' object has no attribute 'main'
我的导入有什么错误吗?
谢谢!
# coding=utf-8
import sys, os
import unittest, mock, kiss
from twisted.python import log
from twisted.trial import unittest
from twisted.cred import credentials
class CredentialsChecker(unittest.TestCase):
"""
Testing the server credentials handling
"""
def _setUp_databases(self):
username_1 = 'user_name'
password_1 = 'user_pass'
email_1 = 'user@mail.org'
create_user_profile = mock.Mock()
self.db = mock.Mock()
self.db.username = username_1
self.db.password = password_1
self.db.email = email_1
def setUp(self):
log.startLogging(sys.stdout)
log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Flushing database")
#management.execute_from_command_line(['manage.py', 'flush',\
#'--noinput'])
log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Populating database")
#management.execute_from_command_line(['manage.py', 'createsuperuser',\
#'--username', 'superuser_name', '--email', 'superuser_name@email.org',\
#'--noinput'])
self._setUp_databases()
log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running tests")
return
"""
Log in with valid credentianls. The server should return True
"""
def test_GoodCredentials(self):
creds = credentials.UsernamePassword('user_name', 'user_pass')
checker = DjangoAuthChecker()
d = checker.requestAvatarId(creds)
if __name__ == '__main__':
unittest.main()
【问题讨论】:
-
您的文件是否命名为
unittest.py?你有这样的文件吗? -
你在哪一行得到错误?
-
打开 Python 解释器。输入
import unittest;unittest.main。它是否打印<class 'unittest.main.TestProgram'>?我认为你的目录结构不正确地隐藏了单元测试。 -
@DavidMašek main 出现在程序中的唯一位置是
unittest.main()调用,所以这肯定是个问题,除非 OP 提供了完全错误的文件。 -
@Kevin 不,文件名是“test_creds.py”
标签: python python-2.7 python-unittest python-mock