测试固件fixture(一):执行范围scope

前言:

在pytest中,固件fixture可以为测试用例提供前置操作。通常情况下,fixture固件会放置在conftest.py文件中。

 

一、定义fixture:

函数加上@pytest.fixture()后,即可成为一个前置操作函数。只需在测试用例中传入该fixture前置操作函数名即可。

 

二、@pytest.fixture()参数介绍:

scope:str类型,应用级别,默认为function。

autouse:布尔类型,自动应用,默认为False。

 

三、scope:

scope的参数值可以是 session,package,module,class, function。

scope='function'

示例:

目录结构:

 

 

test_1.py

def test_1_a(login):
    print("this is test_1_a")
    assert "login" in login


class Test1A:
    def test_1_b(self):
        print("this is test_1_b")

    def test_1_c(self):
        print("this is test_1_c")
test_2.py

def test_2_a():
    print("this is test_2_a")


class Test2A:
    def test_2_b(self, login):
        print("this is test_2_b")
        assert "login" in login

    def test_2_c(self):
        print("this is test_2_c")
test_3.py

def test_3_a():
    print("this is test_3_a")


class Test3A:
    def test_3_b(self):
        print("this is test_3_b")

    def test_3_c(self, login):
        print("this is test_3_c")
        assert "login" in login
test_4.py

def test_4_a():
    print("this is test_4_a")


class Test4A:
    def test_4_b(self):
        print("this is test_4_b")

    def test_1_c(self):
        print("this is test_4_c")
conftest.py

import pytest


@pytest.fixture(scope="function")
def login():
    print("this is login")
    return "login fixture"

  

runner.py

import pytest

if __name__ == '__main__':
    pytest.main(["-s"])

fixture函数login的scope为”function",属于函数/方法级(也可以说是单个用例级)。

执行结果:

test_a\test_1.py this is login
this is test_1_a
.this is test_1_b
.this is test_1_c
.
test_a\test_2.py this is test_2_a
.this is login
this is test_2_b
.this is test_2_c
.
test_b\test_3.py this is test_3_a
.this is test_3_b
.this is login
this is test_3_c
.
test_b\test_4.py this is test_4_a
.this is test_4_b
.this is test_4_c
.

============================= 12 passed in 0.03s ==============================

我们可以看到,若scope='function',则是属于用例级别,每一个使用了fixture的用例将会执行该前置操作(test_1.py中的test_1_a执行前执行了前置操作,test_2.py中的test_2_b执行前执行了前置操作,test_3.py中的test_3_c

执行了前置操作,test_4.py无调用前置操作)。

scope='package'

 

 

test_1.py

def test_1_a():
    print("this is test_1_a")


class Test1A:
    def test_1_b(self):
        print("this is test_1_b")

    def test_1_c(self, login):
        print("this is test_1_c")
        assert "login" in login
test_2.py

def test_2_a():
    print("this is test_2_b")


class Test2B:
    def test_2_b(self, login):
        print("this is test_2_b")
        assert "login" in login

    def test_2_c(self):
        print("this is test_2_c")
@pytest.fixture(scope="session")
def login():
    print("this is login")
    return "login fixture"

我们可以看到,test_1.py,test_2.py 都使用了fixture,但scope为‘session’。

执行结果:


test_a\test_1.py this is test_1_a
.this is test_1_b
.this is login
this is test_1_c
.
test_b\test_2.py this is test_2_b
.this is test_2_b
.this is test_2_c
.

============================== 6 passed in 0.02s ==============================

执行结果表明,fixture前置操作在整个会话(一次执行全过程)中只执行了一次。

同理:

scope='package'的执行等级为包,即整个包只执行一次fixture前置操作。

scope='module'的执行等级为模块,即一个.py文件只执行一次fixture前置操作。

scope='class'的执行等级为类,即一个类只执行一次fixture前置操作。

 

posted @ 2022-02-22 16:37  Target_L  阅读(133)  评论(0)    收藏  举报