遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(06):fixture作用域(scope)详解

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846 

scope参数

表示被@pytest.fixture标记的函数的作用域:

1
2
3
4
5
6
7
8
9
"function":默认值,作用于每个测试用例(包含函数/方法),每个用例执行前都会运行一次
 
"class":作用于整个类,每个测试类/测试函数执行前都会运行一次
 
"module":作用于整个模块(多个类),每个module(每个py文件)执行前都会运行一次;可以实现多个.py跨文件共享前置
 
"package":每个python包执行前都会运行一次
 
"session":作用于整个session,整个测试前运行一次

  

如果fixture放conftest.py中,可以这么说:

1
2
3
4
5
6
7
scope参数为function:每一个测试文件中的所有测试用例执行前都会执行一次conftest文件中的fixture
 
scope参数为class:每一个测试文件中的测试类执行前都会执行一次conftest文件中的fixture
 
scope参数为module:每一个测试文件执行前都会执行一次conftest文件中的fixture
 
scope参数为session:所有测试py文件执行前执行一次conftest文件中的fixture

 

一些总结:

1
2
3
4
5
6
7
8
9
10
11
默认是function
 
执行顺序遵循:sesstion->package->module->class->function
 
每一个函数前后均会执行模块中的class
 
模块中的fixture对函数、方法均有效
 
测试类中的fixture只对方法有效
 
在模块和类中有同名的fixture存在时:局部优先,也就是类中fixture优先

 

下面逐一验证。

 

默认是function

设置默认运行,未指定scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
 
@pytest.fixture(autouse=True)
def fun():
    print("---fixture")
 
def test_a():
    print("--------------test_a")
 
class Test01:
    def test_b(self):
        print("--------------test_b")
 
    def test_c(self):
        print("--------------test_c")

  

结果:作用域默认是function

 

执行顺序遵循:sesstion->package->module->class->function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
 
@pytest.fixture(autouse=True, scope="function")
def fun():
    print("---fixture : function-前")
    yield
    print("---fixture : function-后")
 
@pytest.fixture(autouse=True, scope="class")
def fun2():
    print("---fixture : class-前")
    yield
    print("---fixture : class-后")
 
@pytest.fixture(autouse=True, scope="module")
def fun3():
    print("---fixture : module-前")
    yield
    print("---fixture : module-后")
 
@pytest.fixture(autouse=True, scope="package")
def fun4():
    print("---fixture : package-前")
    yield
    print("---fixture : package-后")
 
@pytest.fixture(autouse=True, scope="session")
def fun5():
    print("---fixture : session-前")
    yield
    print("---fixture : session-后")
 
def test_a():
    print("--------------test_a")
 
def test_b():
    print("--------------test_b")
 
class Test01Scope:
    def test_c(self):
        print("--------------test_c")
 
    def test_d(self):
        print("--------------test_d")

 

运行结果:

 

每一个函数前后均会执行模块中的class

运行结果:

 

模块中的fixture对函数、方法均有效

运行结果:

 

测试类中的fixture只对方法有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
 
def test_a():
    print("--------------test_a")
 
def test_b():
    print("--------------test_b")
 
class Test01Scope:
    def test_c(self):
        print("--------------test_c")
 
    def test_d(self):
        print("--------------test_d")
 
    @pytest.fixture(autouse=True, scope="function")
    def fun(self):
        print("---fixture : function-前")
        yield
        print("---fixture : function-后")
 
    @pytest.fixture(autouse=True, scope="class")
    def fun2(self):
        print("---fixture : class-前")
        yield
        print("---fixture : class-后")
 
    @pytest.fixture(autouse=True, scope="module")
    def fun3(self):
        print("---fixture : module-前")
        yield
        print("---fixture : module-后")
 
    @pytest.fixture(autouse=True, scope="package")
    def fun4(self):
        print("---fixture : package-前")
        yield
        print("---fixture : package-后")
 
    @pytest.fixture(autouse=True, scope="session")
    def fun5(self):
        print("---fixture : session-前")
        yield
        print("---fixture : session-后")

  

结果:对函数无效

 

在模块和类中有同名的fixture存在时:局部优先,也就是类中fixture优先

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
 
def test_a():
    print("--------------test_a")
 
def test_b():
    print("--------------test_b")
 
 
@pytest.fixture(autouse=True, scope="function")
def fun():
    print("---fixture : function-前")
    yield
    print("---fixture : function-后")
 
 
@pytest.fixture(autouse=True, scope="class")
def fun2():
    print("---fixture : class-前")
    yield
    print("---fixture : class-后")
 
 
@pytest.fixture(autouse=True, scope="module")
def fun3():
    print("---fixture : module-前")
    yield
    print("---fixture : module-后")
 
 
@pytest.fixture(autouse=True, scope="package")
def fun4():
    print("---fixture : package-前")
    yield
    print("---fixture : package-后")
 
 
@pytest.fixture(autouse=True, scope="session")
def fun5():
    print("---fixture : session-前")
    yield
    print("---fixture : session-后")
 
class Test01Scope:
    def test_c(self):
        print("--------------test_c")
 
    def test_d(self):
        print("--------------test_d")
 
    @pytest.fixture(autouse=True, scope="function")
    def fun(self):
        print("---fixture : function-前(类中)")
        yield
        print("---fixture : function-后(类中)")
 
    @pytest.fixture(autouse=True, scope="class")
    def fun2(self):
        print("---fixture : class-前(类中)")
        yield
        print("---fixture : class-后(类中)")
 
    @pytest.fixture(autouse=True, scope="module")
    def fun3(self):
        print("---fixture : module-前(类中)")
        yield
        print("---fixture : module-后(类中)")
 
    @pytest.fixture(autouse=True, scope="package")
    def fun4(self):
        print("---fixture : package-前(类中)")
        yield
        print("---fixture : package-后(类中)")
 
    @pytest.fixture(autouse=True, scope="session")
    def fun5(self):
        print("---fixture : session-前(类中)")
        yield
        print("---fixture : session-后(类中)")

 

结果:

 

posted @   全栈测试笔记  阅读(457)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示