04-pytest-fixture作用范围

简介: 04-pytest-fixture作用范围

一、fixture参数介绍

1. def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
2. """
3.      可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
4.      引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。 
5.      测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
6. 
7.     :arg scope: scope 有五个级别参数 "function" (默认), "class", "module" ,"package" or "session".
8. 
9.     :arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
10. 
11.     :arg autouse:  如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
12. 
13.     :arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
14. 
15.     :arg name:   fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
16.                        “fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')。
17. """

二、scope参数控制fixture的作用范围

  • default:不传参数就是默认的,作用域就是所有的函数
  • class:作用域是每个class文件,只会运行一次
  • module:作用域是一个模块,在一个模块中只会运行一次
  • package :作用域是在一个package包中都有效
  • session :在整个会话都有效

 默认作用域

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/8
3. # @Author  : 大海
4. """
5. 新建test_01.py文件
6. """
7. import pytest
8. 
9. 
10. # 默认就是scope="function",它的作用范围是每个测试用例来之前运行一次
11. @pytest.fixture()
12. def open_url():
13. print('打开网址')
14. 
15. 
16. class TestLogin(object):
17. def test_login_success(self, open_url):
18. print('这是登录成功case')
19. 
20. def test_login_error(self, open_url):
21. print('这是登录异常case')
22. 
23. 
24. if __name__ == '__main__':
25.     pytest.main(["-s", "test_01.py"])

 class作用域

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/8
3. # @Author  : 大海
4. import pytest
5. 
6. 
7. @pytest.fixture(scope="class")
8. def open_url():
9. print('打开测试网址')
10. 
11. 
12. class TestLogin(object):
13. def test_login_success(self, open_url):
14. print('这是登录成功case')
15. 
16. # 类中只执行一次,这不会执行打开网址,仅做演示(登出不传参即可)
17. def test_logout(self, open_url):
18. print('这是登出操作')
19. 
20. 
21. if __name__ == '__main__':
22.     pytest.main(["-s", "test_02.py"])

 module作用域

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/8
3. # @Author  : 大海
4. 
5. import pytest
6. 
7. 
8. @pytest.fixture(scope="module")
9. def open_url():
10. print('打开测试网址')
11. 
12. 
13. def test_case(open_url):
14. print('这是case1')
15. 
16. 
17. class TestLogin(object):
18. def test_login_success(self, open_url):
19. print('这是登录成功case')
20. 
21. def test_login_error(self, open_url):
22. print('这是异常登录case')
23. 
24. 
25. if __name__ == '__main__':
26.     pytest.main(["-s", "test_03.py"])

 session作用域

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/8
3. # @Author  : 大海
4. import pytest
5. 
6. 
7. # fixture为session级别是可以跨.py模块调用的,也就是当我们有多个.py文件的用例时候,如果多个用例只需调用一次fixture,那就可以设置为scope="session",
8. @pytest.fixture(scope="session")
9. def open_url():
10. print('打开测试网址,这是session级别的!!')
11. 
12. 
13. class TestLogin(object):
14. def test_login_success(self, open_url):
15. print('这是登录成功case')
16. 
17. def test_login_error(self, open_url):
18. print('这是异常登录case')
19. 
20. 
21. if __name__ == '__main__':
22.     pytest.main(["-s", "test_04.py"])


相关文章
|
测试技术 Python
Pytest前后置以及fixture实战部分
Pytest前后置以及fixture实战部分
56 0
|
测试技术 Python
pytest--fixture
pytest--fixture
|
Web App开发 测试技术
10-pytest-parametrize中使用fixture
10-pytest-parametrize中使用fixture
|
测试技术 Python
pytest fixture装饰器
pytest fixture装饰器
|
测试技术
pytest conftest.py和fixture的配合使用
pytest conftest.py和fixture的配合使用
|
测试技术
pytest学习和使用9-fixture中conftest.py如何使用?
pytest学习和使用9-fixture中conftest.py如何使用?
147 0
pytest学习和使用9-fixture中conftest.py如何使用?