当前位置: 首页 > news >正文

pytest框架_@pytest.fixture()

pytest的几大函数介绍:
@pytest.yield_fixture()
@pytest.mark.xfail()
@pytest.mark.skipif

(1)@pytest.fixture()【主要用两个参数:scope\autouse】

@pytest.fixture函数定义:
fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作

fixture(scope=‘function’,params=None,autouse=False,ids=None,name=None),源码解读:
scope默认fuction,有4个级别:function、class、module、package/session。
autouse默认关闭,fixture(autouse=True):默认在每个测试用例执行被装饰的函数。
params是个形参,可以传入多个参数
name用的少,暂不解读。

@pytest.fixture函数3大功能:
功能1:装饰函数_直接看代码中test_case_8的调用,被fixture标记的函数,函数名可以作为入参放进要执行的函数里面。当前函数会优先执行被标记的函数。跟类的继承相似。

# coding=utf-8
import pytest

@pytest.fixture()
def test_case_3():
    print('---3号用例完成---')

@pytest.fixture()
def test_case_4():
    print('---4号用例完成---')

@pytest.fixture()
def test_case_5():
    print('---5号用例完成---')

@pytest.fixture()
def test_case_6():
    print('---6号用例完成---')

@pytest.fixture()
def test_case_7():
    print('---7号用例完成---')

@pytest.fixture()
def test_case_8():
    print('---8号用例完成---')

# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:

    # 被pytest.fixture()装饰的函数,函数名可以作为变量传递给测试用例,最终在执行测试用例之前执行这个装饰过的函数
    def test_case_1(self, test_case_8):
        print('---1号用例完成---')

    # (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
    @pytest.mark.usefixtures('test_case_7')
    @pytest.mark.usefixtures('test_case_6')
    def test_case_2(self):
        print('---2号用例完成---')


if __name__ == "__main__":
    pytest.main(['-vs', 'test_1.py'])
    
test_1.py::Testlogin001::test_case_1 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---8号用例完成---
---1号用例完成---
PASSED
退出浏览器

功能2:自动执行标记的函数。【被fixture标记的函数,默认不自动执行】
这里一般放在conftest.py文件里面。测试用例执行前,每个测试用例会自动启动浏览器,自动进入当前模块的界面等。

import pytest
 @pytest.fixture(autouse=True) # 设置为默认运行
 def before():
     print("------->before")
     
 class Test_ABC:
     def setup(self):
         print("------->setup")
     def test_a(self):
         print("------->test_a")
         assert 1
         
 if __name__ == '__main__':
     pytest.main("-s  test_abc.py")

conftest.py文件

# coding=utf-8
import pytest
from selenium import webdriver
import logging

logger = logging.getLogger()

@pytest.fixture(autouse=True)
def start_module():
    print('---进入要执行模块的的界面---')

功能3:设置作用域

fixture函数是结合conftest.py的文件来使用的,使用场景:
@pytest.fixture() 被装饰的函数,在每个测试用例前执行一次【如果你想让每个用例都重启浏览器,进入要测试的界面,就用这个】
@pytest.fixture(scope=‘class’) 被装饰的函数,在测试用例的类前面被执行一次。
@pytest.fixture(scope=‘modlue’) 被装饰的函数,在py文件前执行一次。
@pytest.fixture(scope=‘package’) 被装饰的函数,在当前的目录下执行一次。

相关文章:

  • pytest框架_@pytest.mark.usefixtures()
  • pytest框架_conftest.py详解
  • pytest框架_@pytest.mark.parametrize()
  • pytest框架_@pytest.yield_fixture()
  • pytest框架_@pytest.mark.xfail()
  • pytest框架_@pytest.mark.skipif()
  • pytest框架_pytest.ini
  • pytest框架_测试报告(allure-pytest)
  • pytest框架_pytest_runtest_makereport实现失败截图自动加入allure
  • pytest框架_assert断言
  • python_获取共享文件里的内容
  • 算法基础_二分查找
  • 算法基础_数组、链表、选择排序
  • selenium_alert、confirm、prompt弹框、上传文件的弹框
  • selenium_鼠标操作(Action_chains.py)、特殊场景:画布上画多边形
  • 2017前端实习生面试总结
  • Java多态
  • 给Prometheus造假数据的方法
  • 力扣(LeetCode)965
  • 前端存储 - localStorage
  • 前端技术周刊 2019-01-14:客户端存储
  • 无服务器化是企业 IT 架构的未来吗?
  • 新手搭建网站的主要流程
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 字符串匹配基础上
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • # .NET Framework中使用命名管道进行进程间通信
  • (2)Java 简介
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (ZT)一个美国文科博士的YardLife
  • (附源码)计算机毕业设计大学生兼职系统
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (一)基于IDEA的JAVA基础1
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • (转)ABI是什么
  • (转)Oracle存储过程编写经验和优化措施
  • (转)大道至简,职场上做人做事做管理
  • .mysql secret在哪_MySQL如何使用索引
  • .net wcf memory gates checking failed
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .NET程序员迈向卓越的必由之路
  • .net与java建立WebService再互相调用
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • [100天算法】-实现 strStr()(day 52)
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [ai笔记3] ai春晚观后感-谈谈ai与艺术
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [EWS]查找 文件夹
  • [Go WebSocket] 多房间的聊天室(五)用多个小锁代替大锁,提高效率
  • [hdu 2896] 病毒侵袭 [ac自动机][病毒特征码匹配]
  • [hdu1561] The more, The Better 【树形DP】
  • [hive] posexplode函数
  • [ISITDTU 2019]EasyPHP