python - Django REST 单元测试 TypeError: __init__() 接受 1 个位置参数,但给出了 2 个
问题描述
我正在使用命令启动以下代码$ python manage.py test
,它返回错误:
TypeError: __init__() takes 1 positional argument but 2 were given
据我所知,我只是传递self
给__init__ method
,这个额外的 arg 来自哪里?我在这里检查了多个答案并查看了django 文档,但似乎找不到我的错误。
这是什么原因造成的?
代码:
import requests
import json
from django.contrib.auth.models import User
from django.test import TestCase
from django.test import Client
class BasicFunctionality(TestCase):
def __init__(self):
user_name = 'boris_the_blade'
password = 'boris_the_sneaky_russian'
self.client = Client()
self.login_status = self.createUserAndLogin(user_name, password)
def createUserAndLogin(self, user_name, password):
self.user = User.objects.create_user(username=user_name, password=password)
login = self.client.login(username=user_name, password=password)
return login
def test_login(self):
self.assertTrue(self.login_status)
全终端输出:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/core/management/commands/test.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/test/runner.py", line 603, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/home/tompreston/.python_virtualenvs/lagoon-back-end-K1-2r-Ad/lib/python3.6/site-packages/django/test/runner.py", line 514, in build_suite
tests = self.test_loader.discover(start_dir=label, **kwargs)
File "/usr/lib/python3.6/unittest/loader.py", line 341, in discover
tests = list(self._find_tests(start_dir, pattern))
File "/usr/lib/python3.6/unittest/loader.py", line 406, in _find_tests
yield from self._find_tests(full_path, pattern, namespace)
File "/usr/lib/python3.6/unittest/loader.py", line 406, in _find_tests
yield from self._find_tests(full_path, pattern, namespace)
File "/usr/lib/python3.6/unittest/loader.py", line 398, in _find_tests
full_path, pattern, namespace)
File "/usr/lib/python3.6/unittest/loader.py", line 452, in _find_test_path
return self.loadTestsFromModule(module, pattern=pattern), False
File "/usr/lib/python3.6/unittest/loader.py", line 123, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/usr/lib/python3.6/unittest/loader.py", line 92, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "/usr/lib/python3.6/unittest/suite.py", line 24, in __init__
self.addTests(tests)
File "/usr/lib/python3.6/unittest/suite.py", line 57, in addTests
for test in tests:
TypeError: __init__() takes 1 positional argument but 2 were given
解决方案
不要覆盖在数据库中设置测试数据的__init__
方法。TestCase
改为使用setUp
。
def setUp(self):
user_name = 'boris_the_blade'
password = 'boris_the_sneaky_russian'
self.client = Client() # Django's TestCase already sets self.client so this line isn't required
self.login_status = self.createUserAndLogin(user_name, password)
推荐阅读
- python - Pydantic 对 FastAPI 中的良好实践进行建模
- php - 通过 API 上传新产品的 SearchKeywordUpdate 错误
- java - 在 GraalVM 中运行 Python - `import requests` 失败并出现异常:您需要安装 charset_normalizer 或 chardet(请求初始化失败)
- vba - 无法识别名称“来源”。PowerQuery 的 VBA
- perl - Perl 从乌克兰语到英语的翻译
- excel - 水晶报表连续显示数据
- javascript - 反应酶 onclick 期望“onclick 被调用!”
- html - 无法在 Angular 中读取 null 的属性“getBoundingClientRect”
- nginx - 如果我的 Traefik 容器出现故障怎么办?我可以回退到默认的 nginx 吗?
- html - Internet Explorer 11 上未显示网站背景图片