给出Django测试错误参数0

时间:2018-02-22 13:06:11

标签: django testing arguments

我无法运行此功能:

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)

我尝试更改参数的数量,然后我得到了同样的错误PLUS另一个,如下:

======================================================================
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 个答案:

答案 0 :(得分:0)

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

如果愿意,您可以将test_test_custom_mail_server_connection重命名为test_custom_mail_server_connection

答案 1 :(得分:0)

我的错误是我尝试测试的方法被称为axis.ticks.x = element_line(colour = "black", size = c(2, rep(1,6))),我认为问题在于它开始于&#34; test&#34;并将其更改为test_custom_mail_server_connection,但仍然无法正常工作。 解决方案是完全删除&#34; test&#34;方法名称:do_test_custom_mail_server_connection

解决方案: 如果您想测试某个方法,则不应包含“&#34; test&#34;在其中。