简体   繁体   English

在这个 class 中测试这个“setUp()”function 的正确方法是什么?

[英]What's the correct way to test this “setUp()” function inside this class?

It's from the book L earn Algorithmic Trading- Sebastien Donadio and Sourav Ghosh它来自《L Earn Algorithmic Trading》一书——Sebastien Donadio 和 Sourav Ghosh

src full code >> https://github.com/PacktPublishing/Learn-Algorithmic-Trading/tree/master/Chapter7 src 完整代码>> https://github.com/PacktPublishing/Learn-Algorithmic-Trading/tree/master/Chapter7

I'm supposed to test the我应该测试

TradingStrategy.py 交易策略.py

with this有了这个

TradingStrategy_ut.py TradingStrategy_ut.py

From the TradingStrategy_ut.py I have to call the setUp() function to initialize the TradingStrategy class .TradingStrategy_ut.py我必须调用setUp() function 来初始化TradingStrategy class

class TestMarketSimulator(unittest.TestCase):

    def setUp(self):
        self.trading_strategy= TradingStrategy()

I've tried and I don't understand how to make it work because the TradingStrategy class that's in the TradingStrategy.py file takes 3 parameters.我已经尝试过,但我不明白如何使它工作,因为TradingStrategy.py文件中的TradingStrategy class需要3 个参数。

class TradingStrategy:
    def __init__(self, ob_2_ts, ts_2_om, om_2_ts):
        self.orders = []
        self.order_id = 0
        self.position = 0
        self.pnl = 0
        self.cash = 10000
        self.current_bid = 0
        self.current_offer = 0
        self.ob_2_ts = ob_2_ts
        self.ts_2_om = ts_2_om
        self.om_2_ts = om_2_ts

and every time I call the setUp() function I have this error:每次我调用 setUp() function 我都会遇到这个错误:

x = TestMarketSimulator()
x.setUp()

Traceback (most recent call last):
  File "TradingStrategy_ut.py", line 72, in <module>
    x.setUp()
  File "TradingStrategy_ut.py", line 9, in setUp
    self.trading_strategy= TradingStrategy()
TypeError: __init__() missing 3 required positional arguments: 'ob_2_ts', 'ts_2_om', and 'om_2_ts'

SOLUTION :解决方案

python3 -m unittest TradingStrategy_ut.py python3 -m unittest TradingStrategy_ut.py

and also: edited TradingStrategy.py还有:编辑TradingStrategy.py

added None after every parameter在每个参数后添加None

  class TradingStrategy:
        def __init__(self, ob_2_ts=None, ts_2_om=None, om_2_ts=None): 
            self.orders = []
            self.order_id = 0
            self.position = 0
            self.pnl = 0
            self.cash = 10000
            self.current_bid = 0
            self.current_offer = 0
            self.ob_2_ts = ob_2_ts
            self.ts_2_om = ts_2_om
            self.om_2_ts = om_2_ts

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

相关问题 如果可以的话,在其中一个组件内访问 class 复合材料的正确方法是什么? - What is the correct way to access a class composite inside one of its components, if that's even advisable? 测试功能的正确方法 - Correct way to test a function 在Django中传递S3标头的正确方法是什么? - What is the correct way of passing S3 headers inside Django? 在 PyTorch Function 中使用 PyTorch 模块的正确方法是什么? - What is the correct way to use a PyTorch Module inside a PyTorch Function? 在Python中,从变量实例化类的正确方法是什么? - In Python, what's the correct way to instantiate a class from a variable? 在__init__内调用类/函数是正确的吗? - It's correct to call a class/function inside __init__? 用可选参数处理函数重载的正确方法是什么? - What's the correct way to handle function overloading with optional parameters? 减少函数内变量的正确方法是什么? - What's the correct way to decrease a variable within a function? 引用 Python 中子目录之间文件的正确方法和 VSCode 设置是什么 - What's the correct way and VSCode setup to refer to files between subdirectories in Python 运行此类的正确方法是什么? - What is the correct way to run this class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM