python not is函数_Python unittest assertIsNotNone()用法及代码示例

assertIsNotNonePython中的()是单元测试库函数,用于单元测试中以检查输入值是否为None。此函数将使用两个参数作为输入,并根据断言条件返回布尔值。如果输入值不等于无assertIsNotNone()将返回true,否则返回false。

用法: assertIsNotNone(testValue, message)

参数: assertIsNotNone()接受以下列出的两个参数并作解释:

testValue:将测试变量作为输入值以检查是否与None相等

message:作为测试消息失败时显示的消息的字符串语句。

下面列出了两个不同的示例,它们说明了给定assert函数的正面和负面测试案例:

示例1:否定测试用例

Python3

# unit test case

import unittest

class TestMethods(unittest.TestCase):

# test function

def test_negative(self):

firstValue = None

# error message in case if test case got failed

message = "Test value is none."

# assertIsNotNone() to check that if input value is not none

self.assertIsNotNone(firstValue, message)

if __name__ == '__main__':

unittest.main()

输出:

F

======================================================================

FAIL:test_negative (__main__.TestMethods)

----------------------------------------------------------------------

Traceback (most recent call last):

File "p1.py", line 11, in test_negative

self.assertIsNotNone(firstValue, message)

AssertionError:unexpectedly None:Test value is none.

----------------------------------------------------------------------

Ran 1 test in 0.000s

FAILED (failures=1)

示例2:正测试用例

Python3

# unit test case

import unittest

class TestMethods(unittest.TestCase):

# test function

def test_positive(self):

firstValue = "geeks"

# error message in case if test case got failed

message = "Test value is  none."

# assertIsNotNone() to check that if input value is not none

self.assertIsNotNone(firstValue, message)

if __name__ == '__main__':

unittest.main()

输出:

.

----------------------------------------------------------------------

Ran 1 test in 0.000s

OK

参考:https://docs.python.org/3/library/unittest.html


版权声明:本文为weixin_27885313原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。