Pytest之fixture

ITester软件测试小栈

共 4583字,需浏览 10分钟

 · 2020-08-10

VOL 148

10

2020-08

今天距2021年143天

这是ITester软件测试小栈第148次推文

点击上方蓝字“ITester软件测试小栈“关注我,每周一五早上 07:30准时推送。


微信公众号后台回复“资源测试工具包”领取测试资源,回复“微信群”一起进群打怪。


本文4143字,阅读约需11分钟





在上一篇Pytest系列文章:Pytest之断言,主要介绍常用断言方法及异常断言。

以下主要介绍fixture的介绍、调用方式及作用域。


fixture基本介绍



1

fixture概念

fixturepytest 用于将测试前后进行预备、清理工作的代码处理机制。


2

fixture优势

fixture相对于unittest中的setup和teardown来说有以下几点优势:

  • fixure命名更加灵活,局限性比较小;

  • conftest.py 配置里面可以实现数据共享,不需要import就能自动找到一些配置;

  • scope="session"可以实现多个.py跨文件使用一个session来完成多个用例。



3

fixture语法

语法如下:
fixture(callable_or_scope=None, *args, scope="function", params=None, autouse=False, ids=None, name=None)

  • scope:fixture的作用域,默认为function;

  • autouse:默认:False,需要用例手动调用该fixture;如果是True,所有作用域内的测试用例都会自动调用该fixture;

  • name:装饰器的名称,同一模块的fixture相互调用建议写不同的name。



4

定义fixture

定义fixture,在函数上添加@pytest.fixture即可
@pytest.fixture()
def fixture_demo():
    print("这是fixture")


5

如何区分前后置

在pytest中,用yield区分前后置,即yield前面代码为前置,后面代码为后置。
from selenium import webdriver
@pytest.fixture()
def open_browser_init():
    # 前置
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")
    yield driver
    # 后置
    driver.quit()


fixture调用


调用fixture的三种方式。


1

在测试用例中直接调用

将fixture名称作为参数传入测试用例,如果fixture有返回值,那么测试用例将会接收返回值
import pytest
from selenium import webdriver
@pytest.fixture()
def open_browser_init():
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")
    yield driver
    driver.quit()
def test_input(open_browser_init):
    open_browser_init.find_element_by_id("kw").send_keys("ITester")


2

pytest装饰器调用fixture

在测试用例或测试类上方加上@pytest.mark.usefixtures("fixture名称")
import pytest
from selenium import webdriver
@pytest.fixture()
def open_browser_init():
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")
    yield driver #返回driver
    driver.quit()
@pytest.mark.usefixtures("open_browser_init")
def test_input(open_browser_init):  # fixture名称作为参数传入
    open_browser_init.find_element_by_id("kw").send_keys("ITester")


3

autouse调用fixture

fixture有个参数autouse,默认为False

autouse为True时,同一个作用域的所有测试用例都会调用这个fixture。

autouse为False时,需要手动调用fixture。

import pytest

@pytest.fixture(autouse=True)
def fixture_auto():
    print("这是fixture_auto")

def test_demo01():
    print("运行test_demo01")

def test_demo02():
    print("运行test_demo02")


运行结果为:



fixture作用域


fixture里面有个scope参数可以控制fixture的作用范围,scope参数可以是session, module,class,function, 默认为function。

  • session 会话级别:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module;

  • module 模块级别:模块里所有的用例执行前执行一次module级别的fixture;

  • class 类级别 :每个类执行前都会执行一次class级别的fixture;

  • function  函数级别:每个测试用例执行前都会执行一次function级别的fixture。



1

function级别范围


每个测试用例之前运行一次:

@pytest.fixture()
def test_fixture():
    a = "hello"
    print("每个测试用例之前运行一次")
    yield a
def test_01(test_fixture):
    print("这是test_01")
    assert "e" in test_fixture
def test_02(test_fixture):
    print("这是test_02")
    assert "h" in test_fixture


运行结果为:



2

class级别范围


如果一个class里面有多个用例,都调用了此fixture,那么fixture只在此class里所有用例开始前执行一次。

import pytest
@pytest.fixture(scope="class")
def test_fixture():
    a = "hello"
    yield a
@pytest.mark.usefixtures("test_fixture")
class TestDemo:
    def test_demo01(self,test_fixture):
        assert "h" in test_fixture
    def test_demo02(self,test_fixture):
        assert "o" in test_fixture


运行结果为:




3

module级别范围


在当前.py脚本里面所有用例开始前只执行一次。

import pytest
@pytest.fixture(scope="module")
def test_fixture():
    a = "hello"
    print("在当前文件下执行一次")
    yield a
def test_01(test_fixture):
    print("这是test_01")
    assert "e" in test_fixture
@pytest.mark.usefixtures("test_fixture")
class TestDemo:
    def test_demo01(self,test_fixture):
        print("这是test_demo01")
        assert "h" in test_fixture
    def test_demo02(self,test_fixture):
        print("这是test_demo02")
        assert "o" in test_fixture

运行结果为:



4

session级别范围


session级别是可以跨模块调用的,多个模块下的用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里。

conftest.py作用域:放到项目的根目录下就可以全局调用了,如果放到某个package下,那就在改package内有效。

conftest.py的fixture调用方式,无需导入,直接使用。


conftest.py

import pytest
@pytest.fixture()
def test_fixture():
    a = "hello"
    print("这是conftest")
    yield a


test_demo01.py

def test_01(test_fixture):
    print("这是test_01")
    assert "e" in test_fixture


test_demo02.py

def test_02(test_fixture):
    print("这是test_02")
    assert "h" in test_fixture


命令行输入:pytest -v

输出结果如下:


以上
That‘s all
更多系列文章
敬请期待

ITester软件测试小栈
往期内容宠幸


1.Python接口自动化-接口基础(一)

2.Python接口自动化-接口基础(二)


3.Python接口自动化-requests模块之get请求


4.Python接口自动化-requests模块之post请求

5.Python接口自动化之cookie、session应用


6.Python接口自动化之Token详解及应用


7.Python接口自动化之requests请求封装


8.Python接口自动化之pymysql数据库操作


9.Python接口自动化之logging日志


10.Python接口自动化之logging封装及实战

想获取更多最新干货内容
快来星标 置顶 关注
每周一、三、五 07:30见

<<  滑动查看下一张图片  >>



 后台 回复"资源"取干货
回复"微信群"一起打怪升级

测试交流Q群:727998947

点亮一下在看,你更好看
浏览 45
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报