在python中,如何对没有返回值的函数进行单元测试? [英] In python, how to do unit test on a function without return value?
问题描述
我是一个蟒蛇.这些天,我正在推动自己对项目中的某些核心模块进行更完整的单元测试.由于我们总是使用assertEqual"、assertTrue"等方法进行单元测试,因此这些方法都需要被测试函数的返回值,我想知道如何在没有返回值的情况下对某个函数进行简单的单元测试.
I am a pythoner. At these days I am driving myself to do a more complete unit test on some core module in my project. Since we always do unit test with methods 'assertEqual', 'assertTrue' and so on, these methods all require a return value from the function being tested, I am wondering how to do a plain unit test on some function without a return value.
我想在这里展示一个小例子,如何在 HelloTest 中测试函数 def foo(self, msg)?
I would like to show a tiny example here, how to test function def foo(self, msg) in HelloTest?
class HelloTest(object):
def foo(self, msg):
MSG = msg.upper()
self.bar(MSG)
def bar(self, MSG):
print MSG
推荐答案
在这种特殊情况下,我会模拟打印,然后在我的断言中使用模拟.
In this particular case, I would mock print, then then use the mock in my assertion.
在 Python 中,您将使用 Mock 包进行模拟.
In Python, you will use the Mock package to mock.
这篇关于在python中,如何对没有返回值的函数进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!