代码之家  ›  专栏  ›  技术社区  ›  gdrubich

Django测试错误参数0已给定

  •  0
  • gdrubich  · 技术社区  · 8 年前

    运行此函数时遇到问题:

    def test_custom_mail_server_connection(host, port, user, password, use_tls, recipient):
        '''
        Test connection of mailing server with given user params
        returns None if mail is sent successfully, a string containg the error otherwise
        '''
        result = None
        message_list = []
    
        tenant = get_tenant_by_schema_name(connection.schema_name)
    
        # create new mail_connection
        mail_connection = mail.get_connection()
        mail_connection.host = host
        mail_connection.port = port
        mail_connection.username = user
        mail_connection.password = password
        mail_connection.use_tls = use_tls
    
        from_email = '%s <%s>' % (tenant.name, mail_connection.username)
        subject = _(u'%(name)s: SMARTFENSE - Prueba de servidor de correo propio') % {'name': tenant.name}
    
        mail_content = SimpleTemplateResponse("emails/email_server_test.html", {
            'host': host,
            'port': port,
            'user': user,
            'use_tls': use_tls,
        }, content_type="text/html", charset="utf-8").render().content
    
        msg = mail.EmailMessage(
            subject,
            mail_content,
            from_email,
            [recipient]
        )
    
        msg.content_subtype = 'html'
        message_list.append(msg)
    
        try:
            mail_connection.open()
            mail_connection.send_messages(message_list)
        except Exception as e:
            result = "%s" % e
        finally:
            mail_connection.close()
    
        set_mail_connection_config(mail_connection, 'default')
    
        return result
    

    这基本上检查给定的邮件主机是否正确,以便使用它。

    我写了这个测试:

    def test_test_custom_mail_server_connection(self):
            result = test_custom_mail_server_connection('smtp.gmail.com', 587, 'xxxxx@gmail.com',
                                                        'xxxxxxx', True, [])
            self.assertEqual(result, None)
    

    我得到了下一个错误:

    ERROR: Test connection of mailing server with given user params
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Volumes/Datos/Diamo/Smartfense/env/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
        self.test(*self.arg)
      File "/Volumes/Datos/Diamo/Smartfense/env/lib/python2.7/site-packages/nose/util.py", line 620, in newfunc
        return func(*arg, **kw)
    TypeError: test_custom_mail_server_connection() takes exactly 6 arguments (0 given)
    

    我尝试改变参数的数量,然后我得到了相同的错误加上另一个错误,如下所示:

    ======================================================================
    ERROR: test_test_custom_mail_server_connection (lmsplatform.tests.test_services.LmsPlatformServicesTestCase)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Volumes/Datos/Diamo/Smartfense/smartfense/lmsplatform/tests/test_services.py", line 120, in test_test_custom_mail_server_connection
        'xxxxxxxxxx', True,)
    TypeError: test_custom_mail_server_connection() takes exactly 6 arguments (5 given)
    
    ======================================================================
    ERROR: Test connection of mailing server with given user params
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Volumes/Datos/Diamo/Smartfense/env/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
        self.test(*self.arg)
      File "/Volumes/Datos/Diamo/Smartfense/env/lib/python2.7/site-packages/nose/util.py", line 620, in newfunc
        return func(*arg, **kw)
    TypeError: test_custom_mail_server_connection() takes exactly 6 arguments (0 given)
    

    我发现有几个家伙也有同样的问题,但根本没有解决办法。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Alasdair    8 年前

    您的方法 test_custom_mail_server_connection 正在被视为测试。将其重命名为其他名称,例如。 do_custom_mail_server_connection .

    然后可以重命名 test_test_custom_mail_server_connection test\u custom\u mail\u server\u连接 如果你愿意的话。

        2
  •  0
  •   gdrubich    8 年前

    我的错误是我尝试测试的方法被调用了 test_custom_mail_server_connection ,我想问题是它从“test”开始,然后改为 do_test_custom_mail_server_connection 但还是没起作用。 解决方案是完全删除方法名称中的“test”一词: do_custom_mail_server_connection

    解决方案: 如果你想测试一个方法,你不应该在其中包含“测试”这个词。