从0到1精通自动化测试,pytest自动化测试框架,测试用例setup和teardown(三)

一、前言

学过 unittest 的都知道里面用前置和后置 setup 和 teardown 非常好用,在每次用例开始前和结束后都去执行一次

当然还有更高级一点的 setupClass 和 teardownClass,需配合 @classmethod 装饰器一起使用,在做 selenium 自动化的时候,它的效率尤为突然,可以只启动一次浏览器执行多个用例

pytest框架也有类似于setup和teardown的语法,并且还不止这四个

二、用例运行级别

模块级(setup_module / teardown_module)开始于模块始末,全局的

函数级(setup_function / teardown_function)只对函数用例生效(不在类中)

类级(setup_class / teardown_class)只在类中前后运行一次(在类中)

方法级(setup_method / teardown_method)开始于方法始末(在类中)

类里面的(setup / teardown)运行在调用方法的前后

三、函数式

1、setup_function / teardown_function

1.pytest框架支持函数和类两种用例方式
先看函数里面的前置与后置用法:
setup_function / teardown_function (每个用例开始和结束调用一次)

# test_fixt.py

# coding:utf-8
import pytest
# 函数式

''' 测试追风 QQ交流群:743262921(备注222)'''

def setup_function():
    print("setup_function:每个用例开始前都会执行")

def teardown_function():
    print("teardown_function:每个用例结束后都会执行")

def test_one():
    print("正在执行----test_one")
    x = "this"
    assert 'h' in x

def test_two():
    print("正在执行----test_two")
    x = "hello"
    assert hasattr(x, 'check')

def test_three():
    print("正在执行----test_three")
    a = "hello"
    b = "hello world"
    assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_fixt.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO, inifile:
collected 
  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值