下面是一个示例类,它简化了我所拥有的:
类.py
class MyClass(object):
@staticmethod
def getDictionary():
#some calculations, returns a dictionary
def checkConfiguration(self):
#some code
self.getDictionary()
#some other code
return value
现在我正在为checkConfiguration
做一个单元测试:
类测试.py
import class
import unittest
class TestMyClass(unittest.TestCase):
def setUp(self):
self.classTest = class.MyClass()
def test_CheckConfiguration(self):
#What to put here?
原始的CheckConfiguration
调用getDictionary
。有没有办法告诉test_CheckConfiguration(self)
如果调用getDictionary
,它应该自动返回一个我可以输入的字典?
比如:
def test_CheckConfiguration(self):
if method getDictionary is called:
return {'a':123, 'b':22}
checkValue = self.classTest.checkConfiguration()
我知道这在Java中是可能的,尽管我在这方面没有足够的经验。 非常感谢。你知道吗
我认为你需要一个嘲弄的框架。我建议PyMock。你知道吗
以下是您如何使用它:
类测试.py
https://groups.google.com/forum/?fromgroups#!topic/comp.lang.python/WBhc1xAc8Hw建议对测试中的类进行子类化,并重写
__getattribute__
,以您需要的任何方式记录每个调用。不知道还有什么办法。。。你知道吗相关问题 更多 >
编程相关推荐