python unittest

xiaoxiao2021-02-28  79

简介

import unittest class TestStringMethods(unittest.TestCase): # 需要继承TestCase类 def test_upper(self): # 所有的测试方法都需要以test开头 self.assertEqual('foo'.upper(), 'FOO') # assertEqual检测是否相等 def test_isupper(self): self.assertTrue('FOO'.isupper()) # 检测是否为真 self.assertFalse('Foo'.isupper()) # 检测是否为假 def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): # 验证是否引发异常 s.split(2) if __name__ == '__main__': unittest.main()

工作环境fixture

setUp(),tearDown()

断言方法

方法检查说明assertEqual(a, b)a == b#如果相比较的是list,dict等类型会调用特定的函数assertNotEqual(a, b)a != bassertTrue(x)bool(x) 是 TrueassertFalse(x)bool(x) 是 FalseassertIs(a, b)a 是 b是否是同一个对象assertIsNot(a, b)a 是 不是 bassertIsNone(x)x 是 无assertIsNotNone(x)x 是 不是 无assertIn(a, b)a 在 bassertNotIn(a, b)a 不是 在 bassertIsInstance(a, b)isinstance(a, b)assertNotIsInstance(a, b)不是 isinstance(a, b)
转载请注明原文地址: https://www.6miu.com/read-76123.html

最新回复(0)