2014-04-29 76 views
1

單元測試很新穎,所以這可能非常容易,但我不確定如何模仿函數中的自變量。類內部的python單元測試方法

功能我想測試:

class dataFeed: 
    def generateURL(self, ticker, days, period): 
     return "https://www.google.com/finance/getprices?i=" + str(period) + "&p=" + str(days) + "d&f=d,o,h,l,c,v&df=cpct&q=" + ticker 

測試類:

import unittest 
from dataFeed import dataFeed as df 

class TestCases(unittest.TestCase): 
    def test(self): 
     self.assertEqual(df.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL") 

if __name__ == '__main__': 
    unittest.main() 

輸出我得到的是這樣的:

ERROR: test (__main__.TestCases) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\Users\ian\Documents\Capstone\Components\testing.py", line 9, in test 
    self.assertEqual(df.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL") 
TypeError: unbound method generateURL() must be called with dataFeed instance as first argument (got str instance instead) 

回答

3

你要創建的實例dataFeed對象並將其用於測試。

ex。

class TestCases(unittest.TestCase): 
    def test(self): 
     data_feed = dataFeed() 
     self.assertEqual(data_feed.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL")