![](/img/trans.png)
[英]What is the correct way to access a class composite inside one of its components, if that's even advisable?
[英]What's the correct way to test this “setUp()” function inside this class?
它來自《L Earn Algorithmic Trading》一書——Sebastien Donadio 和 Sourav Ghosh
src 完整代碼>> https://github.com/PacktPublishing/Learn-Algorithmic-Trading/tree/master/Chapter7
我應該測試
有了這個
從TradingStrategy_ut.py我必須調用setUp() function 來初始化TradingStrategy class 。
class TestMarketSimulator(unittest.TestCase):
def setUp(self):
self.trading_strategy= TradingStrategy()
我已經嘗試過,但我不明白如何使它工作,因為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
每次我調用 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'
解決方案:
python3 -m unittest TradingStrategy_ut.py
還有:編輯TradingStrategy.py
在每個參數后添加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.