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: 文
from os.path import realpath
import pytest
@pytest.fixture(scope='class', name="get_path_class")
def get_path_class():
print(" ===> get_path_class")
yield
print(' 获得fixture class path: %s' % realpath(__file__))
Demo/testCase/test_conftest/conftest.py代码
#!/usr/bin/python3
# coding=utf-8
# Author: 文
from os.path import realpath
import pytest
@pytest.fixture(scope='function', name="get_path_function")
def get_path_function():
print(" ===> get_path_function")
yield
print(' 获得fixture path function: %s' % realpath(__file__))
Demo/testCase/test_conftest/test_conftest.py代码
#!/usr/bin/python3
# coding=utf-8
# Author: 文
import pytest
@pytest.mark.usefixtures("get_path_class", "get_path_session")
class Test_A():
@pytest.mark.usefixtures("get_path_function")
def test_01(self):
print(" =====> test_01")
@pytest.mark.usefixtures("get_path_function")
def test_02(self):
print(" =====> test_02")
@pytest.mark.usefixtures("get_path_class", "get_path_session")
class Test_B():
@pytest.mark.usefixtures("get_path_function")
def test_03(self):
print(" =====> test_03")
if __name__ == '__main__':
pytest.main(["-s", "test_conftest.py", "-v"])
打印结果如下:
test_conftest.py::Test_A::test_01 PASSED
test_conftest.py::Test_A::test_02 PASSED
test_conftest.py::Test_B::test_03 PASSED
2020-11-29 10:14:25,227 - root - INFO - ===> get_path_session
2020-11-29 10:20:09,285 - root - INFO - 获得登录token: %gh$gf>dfehfeuhfeofh
2020-11-29 10:14:25,227 - root - INFO - ===> get_path_class
2020-11-29 10:14:25,228 - root - INFO - ===> get_path_function
2020-11-29 10:14:25,228 - root - INFO - =====> test_01
2020-11-29 10:14:25,229 - root - INFO - 获得fixture path function: F:\Demo\testCase\test_conftest\conftest.py
2020-11-29 10:14:25,230 - root - INFO - ===> get_path_function
2020-11-29 10:14:25,231 - root - INFO - =====> test_02
2020-11-29 10:14:25,232 - root - INFO - 获得fixture path function: F:\Demo\testCase\test_conftest\conftest.py
2020-11-29 10:14:25,233 - root - INFO - 获得fixture class path: F:\Demo\testCase\conftest.py
2020-11-29 10:14:25,235 - root - INFO - ===> get_path_class
2020-11-29 10:14:25,235 - root - INFO - ===> get_path_function
2020-11-29 10:14:25,236 - root - INFO - =====> test_03
2020-11-29 10:14:25,237 - root - INFO - 获得fixture path function: F:\Demo\testCase\test_conftest\conftest.py
2020-11-29 10:14:25,237 - root - INFO - 获得fixture class path: F:\Demo\testCase\conftest.py
2020-11-29 10:20:09,298 - root - INFO - 测试完成 Log_out