我为项目中的一个应用程序提供了一个简单的测试套件
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()
无法解决此问题
提前感谢所有帮助!
答案 0 :(得分:1)
我不知道客户端已经是TestCase的一个属性,删除类顶部的构造函数调用似乎解决了问题