方案一
可以再模块上方创建一个公共字典,把需要共用的数据存入该字典中,在方法内部根据key从公共字典中取值
缺点:只能再模块内部传递公共数据,跨模块不生效
代码示例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 全字段正常流
import random
import requests
from config.config import API_URL
from tools.data.make_info import make_id
from tools.data.random_tool import random_tell, random_str_abc, random_number
# 创建一个公共字典,字典里边存放要存入的数据
d={"userName":random_str_abc(2) + str(random_number(min=0,max=9,step=1))}
def test_sign_up_1():
# 方法内部根据关键字,例如:userName从公共字典中取值
da = {"phone":random_tell(),"pwd": "shu123","rePwd": "shu123","userName": d["userName"]}
r = requests.request("POST", API_URL + "/signup",json=da)
print(r.text)
print(r.request.body)
assert "userName" in r.text
def test_login_1():
# 方法内部根据关键字,例如:userName从公共字典中取值
da = {
"pwd": "shu123",
"userName": d["userName"]
}
r = requests.request("POST", API_URL + "/login", json=da)
print(r.text)
print(r.request.body)
assert "token" in r.text
方案二
使用pytest框架提供的fixture函数来传递公共数据,集合conftest.py文件可以实现跨模块传递公共数据
代码示例:
conftest.py文件作用范围,当前包及其子包;
conftest.py解决fixture函数跨模块调用的问题
conftest.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest
from tools.data.random_tool import random_str_abc, random_number
# 只要加了@pytest.fixture()装饰器,该方法就是一个fixture函数。fixture执行完成之后,会把
# fixture方法的返回值存入方法名中,pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
@pytest.fixture(scope="session")
def d():
return {"userName":random_str_abc(2) + str(random_number(min=0,max=9,step=1))}
pytest用例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 全字段正常流
import random
import pytest
import requests
from config.config import API_URL
from tools.data.make_info import make_id
from tools.data.random_tool import random_tell, random_str_abc, random_number
# pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
def test_sign_up_1(d):
# 方法内部根据关键字,例如:userName从公共字典中取值
da = {"phone":random_tell(),"pwd": "shu123","rePwd": "shu123","userName": d["userName"]}
r = requests.request("POST", API_URL + "/signup",json=da)
print(r.text)
print(r.request.body)
assert "userName" in r.text
# pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
def test_login_1(d):
# 方法内部根据关键字,例如:userName从公共字典中取值
da = {
"pwd": "shu123",
"userName": d["userName"]
}
r = requests.request("POST", API_URL + "/login", json=da)
print(r.text)
print(r.request.body)
assert "token" in r.text
fixture函数参数讲解
scope
scope 规定了fixture函数的执行时机,分别有以下四个值
- session 项目启动
- module 进入模块之前
- class 进入类之前
- function 执行一个方法之前
代码示例:
conftest.py:
import pytest
# session 项目启动
# module 进入模块之前
# class 进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session")
def scope_session():
print("我是scope_session")
@pytest.fixture(scope="module")
def scope_module():
print("我是scope_module")
@pytest.fixture(scope="class")
def scope_class():
print("我是scope_class")
@pytest.fixture(scope="function")
def scope_function():
print("我是scope_function")
test_demo_1.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class TestDemo1():
def test_demo1(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo1")
def test_demo2(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo2")
class TestDemo2():
def test_demo3(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo3")
def test_demo4(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo4")
test_demo_2.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class TestDemo3():
def test_demo5(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo5")
def test_demo6(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo6")
class TestDemo4():
def test_demo7(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo7")
def test_demo8(self,scope_session,scope_module,scope_class,scope_function):
print("test_demo8")
执行测试用例:
pycharm terminal中执行下列命令
pytest -v -s -k "demo"

执行结果:
(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected
test_case/test_demo_1.py::TestDemo1::test_demo1 我是scope_session
我是scope_module
我是scope_class
我是scope_function
test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 我是scope_function
test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo1 我是scope_class
我是scope_function
test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo2 我是scope_function
test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 我是scope_module
我是scope_class
我是scope_function
test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 我是scope_function
test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 我是scope_class
我是scope_function
test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 我是scope_function
test_demo8
PASSED
=================================================== 8 passed, 12 deselected in 0.37s ===================================================
总结
看上述执行结果,我们可以发现
scope="session"的fixture函数,只在项目启动的时候执行了一次;
scope="module"的fixture函数,只要执行到一个新的模块,都会执行该fixture函数;
scope="class"的fixture函数,只要执行到一个新的类,都会执行该fixture函数;;
scope="function"的fixture函数,只要执行到一个新的方法,都会执行该fixture函数;
autouse
自调用开关:
- True 开启自调用
- False 关闭自调用
代码示例:
conftest.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest
# session 项目启动
# module 进入模块之前
# class 进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session", autouse=True)
def scope_session():
print("我是scope_session")
@pytest.fixture(scope="module", autouse=True)
def scope_module():
print("我是scope_module")
@pytest.fixture(scope="class", autouse=True)
def scope_class():
print("我是scope_class")
@pytest.fixture(scope="function", autouse=True)
def scope_function():
print("我是scope_function")
test_demo_1.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class TestDemo1():
def test_demo1(self):
print("test_demo1")
def test_demo2(self):
print("test_demo2")
class TestDemo2():
def test_demo3(self):
print("test_demo3")
def test_demo4(self):
print("test_demo4")
test_demo_2.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class TestDemo3():
def test_demo5(self):
print("test_demo5")
def test_demo6(self):
print("test_demo6")
class TestDemo4():
def test_demo7(self):
print("test_demo7")
def test_demo8(self):
print("test_demo8")
执行结果
(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected
test_case/test_demo_1.py::TestDemo1::test_demo1 我是scope_session
我是scope_module
我是scope_class
我是scope_function
test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 我是scope_function
test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo3 我是scope_class
我是scope_function
test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo4 我是scope_function
test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 我是scope_module
我是scope_class
我是scope_function
test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 我是scope_function
test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 我是scope_class
我是scope_function
test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 我是scope_function
test_demo8
PASSED
=================================================== 8 passed, 12 deselected in 0.25s ===================================================
总结:
我们把pytest用例的参数列表中,所有fixture函数的引用都删掉了,
但是所有的fixture函数还是被执行了,执行顺序完全遵循scope关键字的定义
如果我们关掉fixture自调用开关
conftest.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest
# session 项目启动
# module 进入模块之前
# class 进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session")
def scope_session():
print("我是scope_session")
@pytest.fixture(scope="module")
def scope_module():
print("我是scope_module")
@pytest.fixture(scope="class")
def scope_class():
print("我是scope_class")
@pytest.fixture(scope="function")
def scope_function():
print("我是scope_function")
执行结果:
(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected
test_case/test_demo_1.py::TestDemo1::test_demo1 test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo3 test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo4 test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 test_demo8
PASSED
=================================================== 8 passed, 12 deselected in 0.21s ===================================================
我们会发现,当我们关闭fixture函数的自调用开关之后,
所有的fixture函数都没有被执行,
这是因为我们在pytest用例的参数列表中没有引用任何fixture;
网友评论