一、unittest是什么?为什么使用unittest?unittest核心的四个概念?
1. 是python自带的一个单元测试框架,也是python里面最基本的单元测试框架,存放在python安装目录下的lib目录下
2. 在本次项目内,使用unittest框架来为我们组织测试用例
3. unittest里面最核心的四个概念:
test case===>测试用例
test fixture===>测试夹具
test runner===>测试执行
test suite===>测试套件
二、unittest的书写规则?
1. 测试文件必须以test开头,如:test_01_unittest入门脚本.py
2.测试类必须继承unittest.TestCase类
3.测试类也必须以Test开头,如:calss TestLogin(unittest.TestCase)
4.测试方法最好以test开头,如:def test_01_login(self)
5.在测试的py文件里面去执行代码,unittest.main()
6.在测试的类里面可以有普通方法,但是普通方法需要被测试方法调用
#unittest入门脚本
import unittest
class TeatLogin(unittest.TestCase):
def test_login(self):
print("普通登录测试")
def test_login_remember(self):
print("记住密码测试")
if __name__ == '__main__':
unittest.main()
三、unittest核心概念之 test fixture===>测试夹具
概念:
test fixture==>测试夹具
语法
方法级别:
setup() 在每一个测试方法执行之前执行 setup 的代码
teardown() 在每一个测试方法执行之后执行 teardown 的代码
类级别:
setupClass() 在每一个测试类执行之前执行的方法 需要使用@classmethod装饰
teardownClass() 在每一个测试类执行之后执行的方法 需要使用@classmethod装饰
#测试夹具实现代码
import unittest
class TestLogin(unittest.TestCase):
def test_login(self):
"""普通登录测试"""
print("普通登录测试")
def test_login_remember(self):
"""记住密码测试"""
print("记住密码测试")
def setUp(self) -&