unittest单元测试框架
** TestCase:测试用例,存放完整的测试用例,包括测试环境搭建、测试代码、测试环境恢复。形成完整测试单元
** Test Suit:测试套件。多个测试用例的集合。
** TestLoader:将Testcase加载到Test Suite。
** Test Runner:执行测试用例。
实例:
* 创建的测试类,必须继承unittest的unittest.TestCase
* 创建的测试用例,必须以test开头
实例代码:
import unittest
# 创建测试类,测试类必须继承unittest.TestCase
class Test_Study1(unittest.TestCase):
# 用例开始在之前执行,例如初始化driver、打开浏览器等
def setUp(self):
print('start======')
# 用例结束之后执行,例如:关闭driver、关闭浏览器等
def tearDown(self):
print('end======')
# 创建测试用例,用例必须以test开头
def test_case1(self):
print(" run test case 1111")
def test_case2(self):
print("run test case 2222")
def test_case3(self):
print("run test case 3333")
if __name__ == "__main__":
unittest.main()
运行结果:
start====== run test case 1111 end====== start====== run test case 2222 end====== start====== run test case 3333 end====== ---------------------------------------------------------------------- Ran 3 tests in 0.000s Process finished with exit code 0
浙公网安备 33010602011771号