单元测试脚本中的ImportError

时间:2015-08-10 17:31:57

标签: python python-2.7 python-unittest python-mock

我试图为我的服务器设置一些测试。

根据朋友的推荐,我想使用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()    

1 个答案:

答案 0 :(得分:0)

如果您创建一个继承自unittest.TestCase类测试的对象,则必须使用本机Python unittest模块。