在python中没有类型的unittest吗? [英] unittest for none type in python?
问题描述
我只是想知道如何测试一个不返回任何东西的函数.例如,说我有这个功能:
I was just wondering how I would go about testing for a function that does not return anything. for example, say I have this function:
def is_in(char):
my_list = []
my_list.append(char)
然后如果我要对其进行测试:
and then if I were to test it:
class TestIsIn(unittest.TestCase):
def test_one(self):
''' Test if one character was added to the list'''
self.assertEqual(self.is_in('a'), and this is where I am lost)
我不知道该函数等于什么,因为我没有返回值 可以与之比较.
I don't know what to assert the function is equal to, since there is no return value that I could compare it to.
assertIn是否可以工作?
would assertIn work?
推荐答案
所有Python函数都返回某些内容.如果不指定返回值,则返回None
.因此,如果您的目标确实是确保某些内容不会返回值,那么您可以说
All Python functions return something. If you don't specify a return value, None
is returned. So if your goal really is to make sure that something doesn't return a value, you can just say
self.assertIsNone(self.is_in('a'))
(但是,这无法区分没有显式返回值的函数和具有return None
的函数.)
(However, this can't distinguish between a function without an explicit return value and one which does return None
.)
这篇关于在python中没有类型的unittest吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!