目錄
一、前言
二、用例運(yùn)行級(jí)別
三、函數(shù)式
1、setup_function / teardown_function
2、setup_module / teardown_module
四、類和方法
五、函數(shù)和類混合
一、前言
學(xué)過(guò) unittest 的都知道里面用前置和后置 setup 和 teardown 非常好用,在每次用例開(kāi)始前和結(jié)束后都去執(zhí)行一次
當(dāng)然還有更高級(jí)一點(diǎn)的 setupClass 和 teardownClass,需配合 @classmethod 裝飾器一起使用,在做 selenium 自動(dòng)化的時(shí)候,它的效率尤為突然,可以只啟動(dòng)一次瀏覽器執(zhí)行多個(gè)用例
pytest框架也有類似于setup和teardown的語(yǔ)法,并且還不止這四個(gè)
二、用例運(yùn)行級(jí)別
模塊級(jí)(setup_module / teardown_module)開(kāi)始于模塊始末,全局的
函數(shù)級(jí)(setup_function / teardown_function)只對(duì)函數(shù)用例生效(不在類中)
類級(jí)(setup_class / teardown_class)只在類中前后運(yùn)行一次(在類中)
方法級(jí)(setup_method / teardown_method)開(kāi)始于方法始末(在類中)
類里面的(setup / teardown)運(yùn)行在調(diào)用方法的前后
三、函數(shù)式
1、setup_function / teardown_function
1.pytest框架支持函數(shù)和類兩種用例方式
先看函數(shù)里面的前置與后置用法:
setup_function / teardown_function (每個(gè)用例開(kāi)始和結(jié)束調(diào)用一次)
# test_fixt.py
# coding:utf-8
import pytest
# 函數(shù)式
''' 程序員曦曦'''
def setup_function():
print("setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行")
def teardown_function():
print("teardown_function:每個(gè)用例結(jié)束后都會(huì)執(zhí)行")
def test_one():
print("正在執(zhí)行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在執(zhí)行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("正在執(zhí)行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
運(yùn)行結(jié)果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO, inifile:
collected 3 items
test_fixt.py setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_one
.teardown_function:每個(gè)用例結(jié)束后都會(huì)執(zhí)行
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_two
Fteardown_function:每個(gè)用例結(jié)束后都會(huì)執(zhí)行
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_three
.teardown_function:每個(gè)用例結(jié)束后都會(huì)執(zhí)行
================================== FAILURES ===================================
__________________________________ test_two ___________________________________
def test_two():
print("正在執(zhí)行----test_two")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_fixt.py:19: AssertionError
===================== 1 failed, 2 passed in 0.03 seconds ======================
2.從結(jié)果可以看出用例執(zhí)行順序:setup_function > 用例1 > teardown_function, setup_function > 用例2 > teardown_function, setup_function > 用例3 > teardown_function
備注:-s參數(shù)是為了顯示用例的打印信息。 -q參數(shù)只顯示結(jié)果,不顯示過(guò)程
2、setup_module / teardown_module
1.setup_module 是所有用例開(kāi)始前只執(zhí)行一次,teardown_module 是所有用例結(jié)束后只執(zhí)行一次
# test_fixt.py
# coding:utf-8
import pytest
# 函數(shù)式
''' 程序員曦曦'''
def setup_module():
print("setup_module:整個(gè).py模塊只執(zhí)行一次")
print("比如:所有用例開(kāi)始前只打開(kāi)一次瀏覽器")
def teardown_module():
print("teardown_module:整個(gè).py模塊只執(zhí)行一次")
print("比如:所有用例結(jié)束只最后關(guān)閉瀏覽器")
def setup_function():
print("setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行")
def teardown_function():
print("teardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行")
def test_one():
print("正在執(zhí)行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在執(zhí)行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("正在執(zhí)行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
2.從運(yùn)行結(jié)果可以看到 setup_module 和 teardown_module 只執(zhí)行了一次
test_fixt.py setup_module:整個(gè).py模塊只執(zhí)行一次
比如:所有用例開(kāi)始前只打開(kāi)一次瀏覽器
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_one
.teardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_two
Fteardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_three
.teardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行
teardown_module:整個(gè).py模塊只執(zhí)行一次
比如:所有用例結(jié)束只最好關(guān)閉瀏覽器
備注:setup_function / teardown_function和setup_module / teardown_module這四種方法是可以任意組合的,用一個(gè)和多個(gè)都可以
四、類和方法
1.setup / teardown 和 unittest 里面的 setup / teardown 是一樣的功能,setup_class 和 teardown_class 等價(jià)于 unittest 里面的 setupClass 和 teardownClass
#test_fixtclass.py
# coding:utf-8
import pytest
# 類和方法
''' 程序員曦曦'''
class TestCase():
def setup(self):
print("setup: 每個(gè)用例開(kāi)始前執(zhí)行")
def teardown(self):
print("teardown: 每個(gè)用例結(jié)束后執(zhí)行")
def setup_class(self):
print("setup_class:所有用例執(zhí)行之前")
def teardown_class(self):
print("teardown_class:所有用例執(zhí)行之前")
def setup_method(self):
print("setup_method: 每個(gè)用例開(kāi)始前執(zhí)行")
def teardown_method(self):
print("teardown_method: 每個(gè)用例結(jié)束后執(zhí)行")
def test_one(self):
print("正在執(zhí)行----test_one")
x = "this"
assert 'h' in x
def test_two(self):
print("正在執(zhí)行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
print("正在執(zhí)行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
運(yùn)行結(jié)果
test_fixtclass.py setup_class:所有用例執(zhí)行之前
setup_method: 每個(gè)用例開(kāi)始前執(zhí)行
setup: 每個(gè)用例開(kāi)始前執(zhí)行
正在執(zhí)行----test_one
.teardown: 每個(gè)用例結(jié)束后執(zhí)行
teardown_method: 每個(gè)用例結(jié)束后執(zhí)行
setup_method: 每個(gè)用例開(kāi)始前執(zhí)行
setup: 每個(gè)用例開(kāi)始前執(zhí)行
正在執(zhí)行----test_two
Fteardown: 每個(gè)用例結(jié)束后執(zhí)行
teardown_method: 每個(gè)用例結(jié)束后執(zhí)行
setup_method: 每個(gè)用例開(kāi)始前執(zhí)行
setup: 每個(gè)用例開(kāi)始前執(zhí)行
正在執(zhí)行----test_three
.teardown: 每個(gè)用例結(jié)束后執(zhí)行
teardown_method: 每個(gè)用例結(jié)束后執(zhí)行
teardown_class:所有用例執(zhí)行之前
2.從結(jié)果看出,運(yùn)行的優(yōu)先級(jí):setup_class > setup_method > setup > 用例 > teardown > teardown_method > teardown_class
備注:這里 setup_method 和 teardown_method 的功能和 setup / teardown功能是一樣的,一般二者用其中一個(gè)即可
五、函數(shù)和類混合
1.如果一個(gè).py的文件里面既有函數(shù)用例又有類和方法用例,運(yùn)行順序又是怎樣的呢?
# coding:utf-8
import pytest
# 類和方法
''' 程序員曦曦'''
def setup_module():
print("setup_module:整個(gè).py模塊只執(zhí)行一次")
print("比如:所有用例開(kāi)始前只打開(kāi)一次瀏覽器")
def teardown_module():
print("teardown_module:整個(gè).py模塊只執(zhí)行一次")
print("比如:所有用例結(jié)束只最后關(guān)閉瀏覽器")
def setup_function():
print("setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行")
def teardown_function():
print("teardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行")
def test_one():
print("正在執(zhí)行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在執(zhí)行----test_two")
x = "hello"
assert hasattr(x, 'check')
class TestCase():
def setup_class(self):
print("setup_class:所有用例執(zhí)行之前")
def teardown_class(self):
print("teardown_class:所有用例執(zhí)行之前")
def test_three(self):
print("正在執(zhí)行----test_three")
x = "this"
assert 'h' in x
def test_four(self):
print("正在執(zhí)行----test_four")
x = "hello"
assert hasattr(x, 'check')
if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
運(yùn)行結(jié)果:
test_fixtclass.py setup_module:整個(gè).py模塊只執(zhí)行一次
比如:所有用例開(kāi)始前只打開(kāi)一次瀏覽器
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_one
.teardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行
setup_function:每個(gè)用例開(kāi)始前都會(huì)執(zhí)行
正在執(zhí)行----test_two
Fteardown_function:每個(gè)用例結(jié)束前都會(huì)執(zhí)行
setup_class:所有用例執(zhí)行之前
正在執(zhí)行----test_three
.正在執(zhí)行----test_four
Fteardown_class:所有用例執(zhí)行之前
teardown_module:整個(gè).py模塊只執(zhí)行一次
比如:所有用例結(jié)束只最后關(guān)閉瀏覽器
2.從運(yùn)行結(jié)果看出,setup_module / teardown_module 的優(yōu)先級(jí)是最大的,然后函數(shù)里面用到的 setup_function / teardown_function 與類里面的 setup_class / teardown_class 互不干涉
? ? ? ? ? 【下面是我整理的2023年最全的軟件測(cè)試工程師學(xué)習(xí)知識(shí)架構(gòu)體系圖】
一、Python編程入門到精通
二、接口自動(dòng)化項(xiàng)目實(shí)戰(zhàn)??
三、Web自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
四、App自動(dòng)化項(xiàng)目實(shí)戰(zhàn)?
五、一線大廠簡(jiǎn)歷
六、測(cè)試開(kāi)發(fā)DevOps體系?
七、常用自動(dòng)化測(cè)試工具
八、JMeter性能測(cè)試?
九、總結(jié)(尾部小驚喜)
生命不息,奮斗不止。每一份努力都不會(huì)被辜負(fù),只要堅(jiān)持不懈,終究會(huì)有回報(bào)。珍惜時(shí)間,追求夢(mèng)想。不忘初心,砥礪前行。你的未來(lái),由你掌握!
生命短暫,時(shí)間寶貴,我們無(wú)法預(yù)知未來(lái)會(huì)發(fā)生什么,但我們可以掌握當(dāng)下。珍惜每一天,努力奮斗,讓自己變得更加強(qiáng)大和優(yōu)秀。堅(jiān)定信念,執(zhí)著追求,成功終將屬于你!
只有不斷地挑戰(zhàn)自己,才能不斷地超越自己。堅(jiān)持追求夢(mèng)想,勇敢前行,你就會(huì)發(fā)現(xiàn)奮斗的過(guò)程是如此美好而值得。相信自己,你一定可以做到!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-493417.html
【軟件測(cè)試技術(shù)交流(資料分享)】:320231853(備注C)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=eU1h9ykztGeBOGy_wSKn0v33ThZlM8Xh&authKey=3aLC55z%2F7ZglBlNu5uwQopOR2WZAlAuFDvJ6sqEnXLJP8y9fvuhBS0p%2FNOTaZyQG&noverify=0&group_code=320231853文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-493417.html
到了這里,關(guān)于從0到1精通自動(dòng)化測(cè)試,pytest自動(dòng)化測(cè)試框架,測(cè)試用例setup和teardown(三)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!