繁体   English   中英

类型错误:test_admin_login() 缺少 1 个必需的位置参数:'self'

[英]TypeError: test_admin_login() missing 1 required positional argument: 'self'

我正在为基于 Django 的 rest API 编写测试用例。 问题出在测试用例类的方法中。 我想在setuptestdata方法中使用test_admin_login方法。 而我无法做到这一点。 我收到了我在标题中提到的错误。

class MyTests(TestCase):
    allow_database_queries: True
    client = APIClient()

    @classmethod
    def setUpTestData(cls):
        # client = APIClient()
        # AUTHENTICATION 
        # response = cls.client.post(reverse('auth'),data={"username":"admin","password":"ppoopp00"},format='json')
        value = cls.test_admin_login()
        print(value)
        cls.auth_token = cls.test_admin_login(cls)

        # CREATE PROJECT
        response = cls.client.post(
            reverse('projects:project_create'),
            data={"platform_subscriber_entity":"NucoinTest"},
            format='json', 
            HTTP_AUTHORIZATION="JWT "+cls.auth_token
            
        )
        cls.project_key  = response.data["platform_subscriber_id"]


    def test_admin_login(self):
        """
        Ensure we can create a new account object.
        """
        url = reverse('auth')
        response = self.client.post(url,data={"username":"admin","password":"testpass"},format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)
        return response.data["access"]

如果任何函数需要self这意味着您需要先创建它的一个实例才能使用它,并且您使用cls而不是实例,所以您只需要创建一个实例

cls().test_admin_login(cls)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM