unittest for none type in python?

℡╲_俬逩灬. 提交于 2019-12-03 22:51:24

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

(However, this can't distinguish between a function without an explicit return value and one which does return None.)

aquavitae

The point of a unit test is to test something that the function does. If its not returning a value, then what is it actually doing? In this case, it doesn't appear to be doing anything, since my_list is a local variable, but if your function actually looked something like this:

def is_in(char, my_list):
    my_list.append(char)

Then you would want to test if char is actually appended to the list. Your test would be:

def test_one(self):
    my_list = []
    is_in('a', my_list)
    self.assertEqual(my_list, ['a'])

Since the function does not return a value, there's no point testing for it (unless you need make sure that it doesn't return a value).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!