引入 我们之前学习的都是测试用例的前置固件,也就是相当于“setup”。说到这,细心的你可能想到了,那有没有什么方式可以表示出“teardown”?这就是我们今天学习的yield和addfinalizer。
yield yield是一个关键字,它不是单独存在的,要写在fixtrue标记的固件中。
我们在声明的固件myfixture中加入yield关键字,在它下面写测试用例执行后想要运行的代码;其他有关于固件的使用没有任何差别。需要说明的一点是我们在pytest主函数中增加了一个参数“–setup-show”,他会显示出固件的执行情况。
import pytest
@pytest.fixture()
def myfixture():
print("执行myfixture前半部分")
yield
print("执行myfixture的后半部分")
class Test_firstFile():
def test_one(self,myfixture):
print("执行test_one")
assert 1+2==3
def test_two(self):
print("执行test_two")
assert 1==1
def test_three(self):
print("执行test_three")
assert 1+1==2
if __name__=="__main__":
pytest.main(["--setup-show","-s","test03.py"])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items
test03.py 执行myfixture前半部分
SETUP F myfixture
test03.py::Test_firstFile::test_one (fixtures used: myfixture)执行test_one
.执行myfixture的后半部分
TEARDOWN F myfixture
test03.py::Test_firstFile::test_two执行test_two
.
test03.py::Test_firstFile::test_three执行test_three
.
============================== 3 passed in 0.03s ==============================
Process finished with exit code 0
fixture里面的teardown用yield来唤醒teardown的执行
@pytest.fixture(scope="function",autouse=True)
def open():
print("打开浏览器,并且打开百度首页")
yield
print("执行teardown!")
def test_s1(open):
print("用例1:搜索python-1",open)
def test_s2():
print("用例2:搜索python-2")
def test_s3():
print("用例3:搜索python-3")
if __name__=="__main__":
pytest.main(["--setup-show","-s","test03.py"])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items
test03.py 打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s1 (fixtures used: open)用例1:搜索python-1 None
.执行teardown!
TEARDOWN F open打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!
TEARDOWN F open打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!
TEARDOWN F open
============================== 3 passed in 0.03s ==============================
Process finished with exit code 0
如果测试用例中的代码出现异常或者断言失败,并不会影响他的固件中yield后的代码执行;但是如果固件中的yield之前的代码也就是相当于setup部分的带代码,出现错误或断言失败,那么yield后的代码将不会再执行,当然测试用例中的代码也不会执行。
@pytest.fixture(scope="function",autouse=True)
def open():
print("打开浏览器,并且打开百度首页")
yield
print("执行teardown!")
def test_s1(open):
print("用例1:搜索python-1",aaaa)
def test_s2():
print("用例2:搜索python-2")
def test_s3():
print("用例3:搜索python-3")
if __name__=="__main__":
pytest.main(["--setup-show","-s","test03.py"])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items
test03.py 打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s1 (fixtures used: open)F执行teardown!
TEARDOWN F open打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!
TEARDOWN F open打开浏览器,并且打开百度首页
SETUP F open
test03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!
TEARDOWN F open
================================== FAILURES ===================================
___________________________________ test_s1 ___________________________________
open = None
def test_s1(open):
> print("用例1:搜索python-1",aaaa)
E NameError: name 'aaaa' is not defined
test03.py:78: NameError
========================= 1 failed, 2 passed in 0.13s =========================
我们也可以通过request.addfinalizer()的方式实现“teardown”
我们在固件中传入request参数;又在固件中定义了一个内置函数;最后将定义的内置函数添加到request的addfinalizer中。
@pytest.fixture()
def myfixture(request):
print ("执行固件myfixture的前半部分")
def myteardown():
print("执行固件myfture的后半部分")
request.addfinalizer(myteardown)
class Test_Pytest():
def test_one(self,myfixture):
print("test_one方法执行" )
assert 1==1
def test_two(self):
print("test_two方法执行" )
assert "o" in "love"
def test_three(self):
print("test_three方法执行" )
assert 3-2==1
if __name__=="__main__":
pytest.main(["--setup-show","-s","test03.py"])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items
test03.py 执行固件myfixture的前半部分
SETUP F myfixture
test03.py::Test_Pytest::test_one (fixtures used: myfixture)test_one方法执行
.执行固件myfture的后半部分
TEARDOWN F myfixture
test03.py::Test_Pytest::test_twotest_two方法执行
.
test03.py::Test_Pytest::test_threetest_three方法执行
.
============================== 3 passed in 0.04s ==============================
Process finished with exit code 0