assertNotInPython中的()是单元测试库函数,用于单元测试中以检查其他字符串是否不包含字符串。此函数将使用三个字符串参数作为输入,并根据断言条件返回一个布尔值。如果 key 不包含在容器字符串中,它将返回true,否则返回false。
用法: assertNotIn(key, container, message)
参数:assertNotIn()接受以下说明的三个参数:
- key:在给定容器中检查其存在性的字符串
- container:在其中搜索关键字符串的字符串
- message:作为测试消息失败时显示的消息的字符串语句。
下面列出了两个不同的示例,它们说明了给定assert函数的正面和负面测试案例:
示例1:否定测试用例
Python3
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_negative(self):
key = "geeks"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is present in container."
# assertNotIn() to check if key is in container
self.assertNotIn(key, container, message)
if __name__ == '__main__':
unittest.main()
输出:
F ====================================================================== FAIL:test_negative (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/e99e85838dde0c8ef29ab17fef478979.py", line 12, in test_negative self.assertNotIn(key, container, message) AssertionError:'geeks' unexpectedly found in 'geeksforgeeks':key is present in container. ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1)
示例2:正测试用例
Python3
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_positive(self):
key = "gfgs"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is present in container."
# assertNotIn() to check if key is in container
self.assertNotIn(key, container, message)
if __name__ == '__main__':
unittest.main()
输出:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
参考:https://docs.python.org/3/library/unittest.html
相关用法
- Python unittest assertNotEqual()用法及代码示例
- Python unittest assertFalse()用法及代码示例
- Python unittest assertIsNot()用法及代码示例
- Python unittest assertIs()用法及代码示例
- Python unittest assertIsNone()用法及代码示例
- Python unittest assertIsNone()用法及代码示例
- Python unittest assertIsNotNone()用法及代码示例
- Python unittest assertEqual()用法及代码示例
- Python unittest assertIn()用法及代码示例
- Python unittest assertNotIsInstance()用法及代码示例
- Python unittest assertIsInstance()用法及代码示例
- Python unittest assertAlmostEqual()用法及代码示例
- Python unittest assertNotAlmostEqual()用法及代码示例
- Python unittest assertLessEqual()用法及代码示例
- Python unittest assertGreaterEqual()用法及代码示例
- Python unittest assertGreater()用法及代码示例
- Python unittest assertTrue()用法及代码示例
- Python unittest assertLess()用法及代码示例
注:本文由纯净天空筛选整理自Shivam.Pradhan大神的英文原创作品 Python unittest – assertNotIn() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。