Python 中“none"类型的单元测试 [英] Unit test for the 'none' type in Python

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

问题描述

我将如何测试不返回任何内容的函数?

How would I 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)

然后如果我要测试它:

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 isn't any return value that I could compare it to.

assertIn 会起作用吗?

推荐答案

所有 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 中“none"类型的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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