Django REST 单元测试 TypeError: __init__() 需要 1 个位置参数

我正在使用命令启动以下代码$ 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)

隔江千里
浏览 173回答 1
1回答

不负相思意

不要覆盖在数据库中设置测试数据的__init__方法TestCase。使用setUp来代替。def setUp(self):&nbsp; &nbsp; user_name = 'boris_the_blade'&nbsp; &nbsp; password = 'boris_the_sneaky_russian'&nbsp; &nbsp; self.client = Client()&nbsp; # Django's TestCase already sets self.client so this line isn't required&nbsp; &nbsp; self.login_status = self.createUserAndLogin(user_name, password)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python