简体   繁体   English

单元测试脚本中的ImportError

[英]ImportError during unittest script

I'm trying to set some tests for my server. 我正在尝试为服务器设置一些测试。

According to a friend's recommendation I wanna use unittest and mock. 根据朋友的建议,我想使用unittest和嘲笑。 For this purpose I wrote some lines but when I try to execute the script I got an importError. 为此,我写了几行,但是当我尝试执行脚本时,遇到了importError。

Traceback (most recent call last):
  File "test_creds.py", line 146, in <module>
    unittest.main()       
AttributeError: 'module' object has no attribute 'main'

Do I have some mistake with the imports? 我的进口商品有误吗?

Thanks! 谢谢!

# 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.TestCase测试类继承的对象,则必须使用本机Python unittest模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM