gpt4 book ai didi

python - Django 应用程序中缺少 runTest 函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:29 25 4
gpt4 key购买 nike

我的项目中的一个应用程序有一个简单的测试套件

from django.test import TestCase
from django.test import Client
import unittest


HTTP_OK = 200
HTTP_NOTFOUND = 404


class XMLTestCase(unittest.TestCase):
"""docstring for XMLTestCase"""

def __init__(self, arg):
super(XMLTestCase, self).__init__()
self.arg = arg


def setUp(self):
self.client = Client()


def test_get_xml(self):
response = self.client.get('get_xml', {'domain': 'sqlconnect', 'stage': 'dev'})
self.AssertEqual(response.status_code, HTTP_OK)
self.AssertEqual(response['Content-Type'], 'text/xml')


def test_get_xml_notfound(self):
response = self.client.get('get_xml', {'domain': 'notadomain', 'stage': 'dev'})
self.AssertEqual(response.status_code, HTTP_NOTFOUND)


def test_upload_xml(self):
f = open('sms_domain_sqlconnecttest.xml', 'r')
response = self.client.post('test_upload_xml', {'xml_string': f.read, 'jobname': 'DRY_RUN'})
self.AssertEqual(response.status_code, HTTP_OK)
self.AssertEqual(response.content, 'File was uploaded')

当我通过调用 python manage.py test 运行单元测试时,这似乎失败了:

  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/test/runner.py", line 533, in run_tests
result = self.run_suite(suite)
File "/home/user/tireis/sqlconnect_ws/sqlconnect-dev/pyvenv/lib64/python3.4/site-packages/django/test/runner.py", line 494, in run_suite
).run(suite)
File "/home/fds/lang/python/lib64/python3.4/unittest/runner.py", line 168, in run
test(result)
File "/home/fds/lang/python/lib64/python3.4/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/home/fds/lang/python/lib64/python3.4/unittest/suite.py", line 122, in run
test(result)
File "/home/fds/lang/python/lib64/python3.4/unittest/case.py", line 663, in __call__
return self.run(*args, **kwds)
File "/home/fds/lang/python/lib64/python3.4/unittest/case.py", line 593, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'XMLTestCase' object has no attribute 'runTest'

添加 if __name__ == '__main__': unittest.main() 不会解决这个问题

在此先感谢您的所有帮助!

最佳答案

我不知道 client 已经是 TestCase 的属性,删除类顶部的构造函数调用似乎可以解决问题

关于python - Django 应用程序中缺少 runTest 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com