pytest框架实现前后置

setup/teardown,setup_class/teardown_class

setup/teardown和setup_class/teardown_class

import pytest
import time


class TestLogin:
    age = 17

    def setup_class(self):
        print("在每个类执行前的初始化工作:比如:创建日志对象,创建数据库对象,创建接口对象")

    # 在每个测试方法之前都会执行
    def setup(self):
        print("\n在执行测试用例之前初始化的代码,打开浏览器,加载网页")

    @pytest.mark.smoke
    def test_01_login(self):
        print("测试login。。。")

    def test_02_regis(self):
        print("i crazy")

    @pytest.mark.skip(reason="this is not dragon")
    def test_05_drogn(self):
        print('this is parper')

    @pytest.mark.skipif(age<18,reason='还未成年')
    def test_04_huahua(self):
        print('hello my name is xxx')

    @pytest.mark.usermanage
    def test_03_haha(self):
        print('hahahahahahahah')

    def teardown(self):
        print("\n在执行测试用例之后的扫尾代码:关闭浏览器")

    def teardown_class(self):
        print("在每个类执行之后的扫尾工作:比如:销毁的日志对象,销毁数据库对象,创建接口的请求对象")


if __name__ == "__main__":
    pytest.main()


pytest.fixture()装饰器来实现部分用例的前后置

@pytest.fixture(scope=‘function’, params=‘’, autouse=‘’, name=‘’)

  • scope表示的是被@pytest.fixture标记方法的作用域。默认是function,class,model,package,session
  • params:参数化(list,tuple,字典列表[{},{},{}],字典元组({},{},{}))
  • autouse:自动执行,默认是false
  • ids:当使用params,给每一个值设置一个变量名,意义不大
  • name:给被@pytest.fixture标记的方法取一个别名

scope=‘function’

手动调用前后置的方法,想让那些方法执行前后置就让那些方法执行

# coding:utf-8
import pytest

@pytest.fixture(scope='function')
def my_fixture():
    print('前置的方法是,实现部分以及全部用例的前置')
    yield
    print('用例后置的方法,实现部分以及全部用例的后置')


class 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值