簡體   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