[英]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我应该测试
with this有了这个
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.