Time will tell.
我们在往期的章节中有讲到过fixture
通过scope
参数控制setup
级别。既然有setup
作为用例之前前的操作,用例执行完之后那肯定也有teardown
操作。用到fixture
的teardown
操作并不是独立函数,用yield
关键字呼唤teardown
操作。
一、scope=”module”
1.fixture
参数 scope=”module” ,module
作用是整个.py
文件都会生效,用例调用时,参数写上函数名称就行。
# 新建一个文件test_f1.py
# coding:utf-8
import pytest
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
def test_s1(open):
print("用例1:搜索python-1")
def test_s2(open): # 不传login
print("用例2:搜索python-2")
def test_s3(open):
print("用例3:搜索python-3")
if __name__ == "__main__":
pytest.main(["-s", "test_f1.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
collected 3 items
..\..\..\..\..\..\YOYO\test_f1.py 打开浏览器,并且打开百度首页
.用例1:搜索python-1
.用例2:搜索python-2