fixture作用范围
fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function
fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""使用装饰器标记fixture的功能
可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。
测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
:arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
:arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
:arg autouse: 如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
:arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
:arg name: fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
“fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')“”。
- function 每一个函数或方法都会调用
- class 每一个类调用一次,一个类可以有多个方法
- module,每一个.py文件调用一次,该文件内又有多个function和class
- session 是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
scope="function"
@pytest.fixture()如果不写参数,默认就是scope=“function”,它的作用范围是每个测试用例之前运行一次,销毁代码在测试用例运行之后运行
#test_fix1.py
import pytest
@pytest.fixture()
def user():
a="admin"
print("获取用户名")
return a
@pytest.fixture(scope="function")
def pwd():
print("获取用户密码")
p="888888"
return p
def test_t1(user,pwd):
print("用户名:{},密码:{}".format(user,pwd))
assert pwd!=None
def test_t2(pwd):
print("用户密码:%s"%pwd)
if __name__=="__main__":
pytest.main(["test_fix1.py"])
运行结果:
用例放到类里面也一样
#test_fix1.py
import pytest
@pytest.fixture()
def user():
a="admin"
print("获取用户名")
return a
@pytest.fixture(scope="function")
def pwd():
print("获取用户密码")
p="888888"
return p
class Test_T:
def test_t1(self,user,pwd):
print("用户名:{},密码:{}".format(user,pwd))
assert pwd!=None
def test_t2(self,pwd):
print("用户密码:%s"%pwd)
assert pwd=="888888"
if __name__=="__main__":
pytest.main(["test_fix1.py"])
运行结果:
scope="class"
fixture为class级别的时候,如果一个class里面有多个用例,都调用了此fixture,那么此fixture只在该class里所有用例开始前执行一次。
#test_fix1.py
import pytest
@pytest.fixture()
def user():
a="admin"
print("获取用户名")
return a
@pytest.fixture(scope="class")
def pwd():
print("class获取用户密码")
p="888888"
return p
def test_f1(user,pwd):
print("test_f1,{},{}\n".format(user,pwd))
class Test_T:
def test_t1(self,user,pwd):
print("test_t1,用户名:{},密码:{}".format(user,pwd))
assert pwd!=None
def test_t2(self,pwd):
print("test_t2,用户密码:%s"%pwd)
assert pwd=="888888"
if __name__=="__main__":
pytest.main(["test_fix1.py"])
运行结果:
发现scope=“class”的,在类外面也可以调用
scope="module"
fixture为module级别时,在当前.py脚本里所有用例在开启前只执行一次
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def one():
a="admin"
print("获取用户名")
return a
def test_f1(one):
print("test_f1,{}\n".format(one))
class Test_T:
def test_t1(self,one):
print("test_t1,名:{}".format(one))
assert one!=None
def test_t2(self,one):
print("test_t2,名:%s"%one)
assert one
if __name__=="__main__":
pytest.main(["test_fix1.py"])
运行结果:
scope="session"
fixture为session级别时可以跨.py模块调用的,也就是当我们有多个.py文件的用例时候,如果多个用例只需调用一次fixture,那就可以设置scope=“session”,并且写到conftest.py文件里。
conftest.py文件名称是固定的,pytest会自动识别该文件。放到工程的根目录下,就可以全局调用了,如果放到某个package包下,那就只在该package内有效。
#conftest.py
import pytest
@pytest.fixture(scope="session")
def the():
print("\n 获取名字,session级别的scope")
a="huazai"
return a
#test_f1.py
import pytest
def test_1(the):
print("\n test_1,名字:{}".format(the))
class Test_C:
def test_2(self,the):
print("test_2,名字:%s"%the)
if __name__=="__main__":
pytest.main("test_f1.py")
#test_f2.py
import pytest
def test_t1(the):
print("\n test_t1,名字:{}".format(the))
class Test_C:
def test_t2(self,the):
print("test_t2,名字:%s"%the)
if __name__=="__main__":
pytest.main(["test_f2.py"])
如果想同时运行test_f1.py, test_f2.py,在cmd执行
pytest -s test_f1.py test_f2.py
运行结果:
pytest(二十三)--conftest.py作用范围
前言
一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用。
在不同的测试子目录也可以放conftest.py,作用范围只在该层级及以下目录生效。
conftest层级关系
在web_item_py项目工程下建两个子项目(包)QQ、UC,并且每个目录下都放一个conftest.py和__init__.py(python的每个package必须要有__init__.py)
案例分析
web_item_py工程下conftest.py文件代码如下:
#web_item_py/conftest.py
# coding:utf-8
import pytest
@pytest.fixture(scope="session")
def begin():
print("\n打开首页")
QQ目录下conftest.py文件代码如下:
#web_item_py/qq/conftest.py
# coding:utf-8
import pytest
@pytest.fixture(scope="session")
def open_baidu():
print("打开百度页面_session")
QQ目录下test_1_qq.py代码如下;
#web_item_py/qq/test_1_qq.py
# coding:utf-8
import pytest
def test_q1(begin,open_baidu):
print("测试用例test_q1")
assert 1
def test_q2(begin,open_baidu):
print("测试用例test_q2")
assert 1
if __name__=="__main__":
pytest.main(["-s","test_1_QQ.py"])
运行QQ目录下test_1_qq.py,结果可以看出,begin和open_baidu是session级别的,只运行一次。
UC目录下conftest.py和test_1_UC.py代码如下
#web_item_py/UC/conftest.py
# coding:utf-8
import pytest
@pytest.fixture(scope="function")
def go():
print("打开搜狗页面")
#web_item_py/UC/test_1_UC.py
# coding:utf-8
import pytest
def test_u1(begin,go):
print("测试用例u1")
assert 1
def test_u2(begin,go):
print("测试用例u2")
assert 2
def test_u3(begin,open_baidu):
print("测试用例u3")
assert 2
if __name__=="__main__":
pytest.main(["-s","test_1_UC.py"])
运行结果:begin起到全局作用,UC目录下的go是function级别,每个用例调用一次。
test_u3(begin,open_baidu)用例不能夸模块调用QQ模块下的open_baidu,所以test_u3用例会运行失败。
同时指向web_item_py工程下的所有用例