转自:https://www.cnblogs.com/poloyy/
一、conftest.py 基础
1.1 什么是conftest.py
可以理解成一个专门存放fixture的配置文件
1.2、实际开发场景
多个测试用例文件(test_*.py)的所有用例都需要用登录功能来作为前置操作
,那就不能把登录功能写到某个用例文件中去了
1.3、如何解决上述场景问题?
conftest.py的出现,就是为了解决上述问题,单独管理一些全局的fixture
1.4、conftest.py 配置 fixture 注意事项
- pytest会默认读取conftest.py里面的所有fixture
- conftest.py 文件名称是固定的,不能改动
- conftest.py只对同一个package下的所有测试用例生效
- 不同目录可以有自己的conftest.py,一个项目中可以有多个conftest.py
- 测试用例文件中不需要手动import conftest.py,pytest会自动查找
二、案例
这是一个目录
2.1 06conftest 目录下
2.1.1 conftest.py代码
此处的 conftest.py 是最顶层的 conftest,一般写全局的 fixture,在 web UI 自动化中,可能会初始化 driver
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
@pytest.fixture(scope="session")
def login():
print("====登录功能,返回账号,token===")
name = "testyy"
token = "npoi213bn4"
yield name, token
print("====退出登录!!!====")
@pytest.fixture(autouse=True)
def get_info(login):
name, token = login
print(f"== 每个用例都调用的外层fixture:打印用户token: {token} ==")
2.1.2 test_1.py 代码
同级目录下的第一条测试用例
def test_get_info(login):
name, token = login
print("***基础用例:获取用户个人信息***")
print(f"用户名:{name}, token:{token}")
2.1.3 06_run.py 代码
运行 06conftest目录下所有测试用例
import pytest
if __name__ == '__main__':
pytest.main(["-s", "../06conftest/"])
2.2 test_51job 目录下
2.2.1 conftest.py代码
配置一些针对 51 job 这个网站的测试用例独有的fixture,譬如:打开51job网站
import pytest
@pytest.fixture(scope="module")
def open_51(login):
name, token = login
print(f"###用户 {name} 打开51job网站###")
2.2.2 test_case1.py代码
某个功能模块下的测试用例
def test_case2_01(open_51):
print("51job,列出所有职位用例")
def test_case2_02(open_51):
print("51job,找出所有python岗位")
2.3 test_toutiao目录下
2.3.1 test_case1.py
包没有__init__.py文件也没有conftest.py文件
def test_no_fixture(login):
print("==没有__init__测试用例,我进入头条了==", login)
2.4 test_weibo 目录下
2.4.1 conftest.py 代码
配置一些针对weibo这个网站的测试用例独有的fixture,譬如:打开weibo网站
import pytest
@pytest.fixture(scope="function")
def open_weibo(login):
name, token = login
print(f"&&& 用户 {name} 返回微博首页 &&&")
2.4.2 test_case1.py代码
某个功能模块下的测试用例
class TestWeibo:
def test_case1_01(self, open_weibo):
print("查看微博热搜")
def test_case1_02(self, open_weibo):
print("查看微博范冰冰")