05-pytest-通过confest.py共享fixture

简介: 05-pytest-通过confest.py共享fixture

使用场景

  • 多个.py文件调用的公共功能可抽取出来,写成配置文件形式读取confest.py的配置

代码示例

 confest.py文件

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/9
3. # @Author  : 大海
4. import pytest
5. 
6. """
7. 注意以下3点:
8.    conftest.py配置脚本名称是固定的,不能改名称
9.    conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
10.    不需要import导入 conftest.py,pytest用例会自动查找
11. """
12. 
13. 
14. @pytest.fixture()
15. def open_url():
16. print("打开网址!")

 注意:

  • conftest.py配置脚本名称是固定的,不能改名称
  • conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
  • 不需要import导入 conftest.py,pytest用例会自动查找

 test_05.py

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/9
3. # @Author  : 大海
4. import pytest
5. 
6. 
7. def test_login(open_url):
8. print('这是登录成功case')
9. 
10. 
11. def test_other():
12. print('这是未登录状态case')
13. 
14. 
15. if __name__ == '__main__':
16.     pytest.main(["-s", "test_05.py"])

 test_06.py

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/9
3. # @Author  : 大海
4. import pytest
5. 
6. 
7. def test_login(open_url):
8. print('这是登录成功test_06')
9. 
10. 
11. def test_other():
12. print('这是未登录状态case')
13. 
14. 
15. if __name__ == '__main__':
16.     pytest.main(["-s", "test_06.py"])

 说明:运行上面两个文件,都可以调用到open_url方法,这样就实现抽取公共方法

相关文章
|
10月前
|
缓存 测试技术
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
33-pytest-内置fixture之pytestconfig使用
33-pytest-内置fixture之pytestconfig使用
|
5天前
|
测试技术 Python
pytest中的fixture和conftest.py
pytest中的fixture和conftest.py
|
8月前
|
测试技术 数据库连接 Python
conftest.py是什么?该怎么用?
conftest.py是什么?该怎么用?
112 0
|
9月前
|
测试技术 Python
pytest--fixture
pytest--fixture
06-pytest-fixture的三种调用方式
06-pytest-fixture的三种调用方式
|
10月前
|
Web App开发 测试技术
10-pytest-parametrize中使用fixture
10-pytest-parametrize中使用fixture
|
12月前
|
测试技术
pytest conftest.py和fixture的配合使用
pytest conftest.py和fixture的配合使用
|
测试技术
pytest学习和使用9-fixture中conftest.py如何使用?
pytest学习和使用9-fixture中conftest.py如何使用?
104 0
pytest学习和使用9-fixture中conftest.py如何使用?