conftest.py
是什么? 可以理解成专门存放fixture
的配置文件,单独管理一些全局的fixture
运用场景
自动化测试过程中所有用例都需要用登录功能来作为前置操作, 那就可以把登录功能通过fixture
写到conftest.py
, 全局调用
注意事项
1. pytest会默认读取conftest.py里面的所有fixture
2. conftest.py 文件名称是固定的,不能修改
3. conftest.py只对该目录下的测试用例与子目录中的测试用例生效
4. 不同目录可以有自己的conftest.py, 一个项目中可以有多个conftest.py
5. 测试用例文件中不需要手动import conftest.py, pytest会自动查找
6. 不同级别的conftest.py文件fixture的name参数相同,子目录下的fixture会覆盖父目录下的fixture
案例
Demo
|__testCase
| |__test_conftest
| | |__test_01.py
| | |__conftest.py
| |__conftest.py
|__conftest.py
Demo/conftest.py代码
#!/usr/bin/python3
# coding=utf-8
# Author: 文
import pytest
@pytest.fixture(scope='session', name="get_path_session")
def get_path_session():
print(" ===> get_path_session")
token = '%gh$gf>dfehfeuhfeofh'
print(" 获得登录token: %s" % token)
yield
print(" 测试完成 Log_out")
Demo/testCase/conftest.py代码
#!/usr/bin/python3
# coding=utf-8
# Author: 文