unittest单元测试框架

一、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) -&
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值