发生“缺少一个位置参数”错误,但所有参数都存在

原始代码有效:


import requests

from api.signals_add import SignalsAdd


class TestLogin:


    def test_log_in(self):

        url = 'https://api.stg.nuroblock.com/api/admin/auth/login'

        data = {"email": "admin02@thinkmelius.com", "password": "123123"}


        r = requests.post(url, json=data)

        assert 200 == r.status_code


    def test_create_signal(self):

        json_for_create_signal = {

                "signalType": "crypto",

                "currencyFrom": "5c1e4633b140f7000f908897",

                "currencyTo": "5c1e4633b140f7000f908898",

                "currencyPair": "5cbd7faf496a8c001124ed5b",

                "type": "sell",

                "buyTip": {"value": 21313},

                "stopTip": 21312.9999,

                "stopTipPips": "-1",

                "takeProfits": [{"value": 21313.0111, "isAchieved": False, "closeOnReach": False, "pips": "+111"}],

                "status": "active",

                "orderType": "market"

        }


        result = requests.post("https://api.stg.nuroblock.com/api/admin/signals",

                               json=json_for_create_signal,

                               headers={'Authorization': 'Bearer ea3d14e631683062073186622d58d2a16HSGB9JDC0ZYtELpwEGL8eNma36EdXei/B72NOa5Y5ln0Sn3+BsWoZdNxK7L2LO4',

                                        'Content-Type': 'application/json'})

                              'Content-Type': 'application/json'})

但是,当我将它分为两个类 - API 类和 Test 类时,找不到最后一个参数


API类

import requests

class SignalsAdd:


    def create_signal(self, signalType, currencyFrom, currencyTo, currencyPair,

                            type, buyTip, stopTip, stopTipPips, takeProfits_value,

                            isAchieved, closeOnReach, pips,

                            status, orderType):

        }


呼如林
浏览 180回答 2
2回答

ABOUTYOU

SignalsAdd.create_signal(...SignalsAdd是一个类,而不是该类的实例。当尝试以这种方式调用该方法时,它被视为普通函数;因此字符串"crypto"成为self(而不是signalType)的值,等等。一个更简单的例子:>>> class x:...&nbsp; &nbsp;def func(self, y):...&nbsp; &nbsp; &nbsp;print('self:', self, 'y:', y)...>>> x.func(2) # wrongTraceback (most recent call last):&nbsp; File "<stdin>", line 1, in <module>TypeError: func() missing 1 required positional argument: 'y'>>> x.func(1, 2) # trying to fix it, but not really properself: 1 y: 2>>> x().func(2) # create an x instance, and use its methodself: <__main__.x object at 0x0000027AF1057320> y: 2或者,可以在没有实例的情况下调用的东西@classmethod(类将作为第一个参数而不是实例传递;这仍然可以让您更改子类的行为):>>> class x:...&nbsp; &nbsp;@classmethod...&nbsp; &nbsp;def func(cls, y): # we rename the argument for clarity....&nbsp; &nbsp; &nbsp;print('class:', cls, 'y:', y)...>>> x.func(2) # It works with either the class...class: <class '__main__.x'> y: 2>>>>>> x().func(2) # or an instance; the instance's class is looked up.class: <class '__main__.x'> y: 2或者 as @staticmethod(什么都没有传递,它只是将一个普通函数放入类的命名空间):>>> class x:...&nbsp; &nbsp;@staticmethod...&nbsp; &nbsp;def func(y):...&nbsp; &nbsp; &nbsp;print('y:', y)...>>> x.func(2) # it also works either way, but there is no extra valuey: 2>>> x().func(2) # passed automatically, so no way to check the subclass.y: 2但是,很有可能您一开始并不真正想要上课。这不是Java;顶层的普通函数工作得很好,并且通常是完成这项工作的最佳工具。

慕桂英3389331

我认为明确传递 self 可能会导致问题。在调用函数时,尝试使用变量名传递值,看看你得到了什么。在这里,您至少会知道哪个值传递给哪个变量以及缺少什么。像这样&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add_signal&nbsp;=&nbsp;SignalsAdd.create_signal(signal_type="crypto",&nbsp;currencyfrom="5c1e4633b140f7000f908897",&nbsp;...)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python