类内的python单元测试方法 [英] python unit testing methods inside of classes

查看:31
本文介绍了类内的python单元测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单元测试非常新,所以这可能很容易,但我不知道如何在函数中模仿 self 参数.

Very new to unit testing so this could be something very easy but I am not sure how to mimic self argument in functions.

我想测试的功能:

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()

我得到的输出是这样的:

the output I get is this:

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)

推荐答案

您需要创建 dataFeed 对象的实例并将其用于测试.

You'll want to create an instance of the dataFeed object and use it for testing.

例如

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")

这篇关于类内的python单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆