轉載地址:https://www.cnblogs.com/yoyoketang/p/9374957.html
前言
學過unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例開始前和結束后都去執行一次。
當然還有更高級一點的setupClass和teardownClass,需配合@classmethod裝飾器一起使用,在做selenium自動化的時候,它的效率尤為突出,可以只啟動一次瀏覽器執行多個用例。
pytest框架也有類似於setup和teardown的語法,並且還不止這四個
用例運行級別
-
模塊級(setup_module/teardown_module)開始於模塊始末,全局的
-
函數級(setup_function/teardown_function)只對函數用例生效(不在類中)
-
類級(setup_class/teardown_class)只在類中前后運行一次(在類中)
-
方法級(setup_method/teardown_method)開始於方法始末(在類中)
-
類里面的(setup/teardown)運行在調用方法的前后
函數式
setup_function/teardown_function
1.pytest框架支持函數和類兩種用例方式,先看函數里面的前置與后置用法:
setup_function/teardown_function 每個用例開始和結束調用一次
# test_fixt.py # coding:utf-8 import pytest # 函數式 ** 作者:上海-悠悠 QQ交流群:588402570** def setup_function(): print("setup_function:每個用例開始前都會執行") def teardown_function(): print("teardown_function:每個用例結束后都會執行") def test_one(): print("正在執行----test_one") x = "this" assert 'h' in x def test_two(): print("正在執行----test_two") x = "hello" assert hasattr(x, 'check') def test_three(): print("正在執行----test_three") a = "hello" b = "hello world" assert a in b if __name__ == "__main__": pytest.main(["-s", "test_fixt.py"])
運行結果:
============================= 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:每個用例開始前都會執行 正在執行----test_one .teardown_function:每個用例結束后都會執行 setup_function:每個用例開始前都會執行 正在執行----test_two Fteardown_function:每個用例結束后都會執行 setup_function:每個用例開始前都會執行 正在執行----test_three .teardown_function:每個用例結束后都會執行 ================================== FAILURES =================================== __________________________________ test_two ___________________________________ def test_two(): print("正在執行----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.從結果可以看出用例執行順序:setup_function》用例1》teardown_function, setup_function》用例2》teardown_function, setup_function》用例3》teardown_function
備注:-s參數是為了顯示用例的打印信息。 -q參數只顯示結果,不顯示過程
setup_function/teardown_function
1.setup_module是所有用例開始前只執行一次,teardown_module是所有用例結束后只執行一次
# test_fixt.py # coding:utf-8 import pytest # 函數式 ** 作者:上海-悠悠 QQ交流群:588402570** def setup_module(): print("setup_module:整個.py模塊只執行一次") print("比如:所有用例開始前只打開一次瀏覽器") def teardown_module(): print("teardown_module:整個.py模塊只執行一次") print("比如:所有用例結束只最后關閉瀏覽器") def setup_function(): print("setup_function:每個用例開始前都會執行") def teardown_function(): print("teardown_function:每個用例結束前都會執行") def test_one(): print("正在執行----test_one") x = "this" assert 'h' in x def test_two(): print("正在執行----test_two") x = "hello" assert hasattr(x, 'check') def test_three(): print("正在執行----test_three") a = "hello" b = "hello world" assert a in b if __name__ == "__main__": pytest.main(["-s", "test_fixt.py"])
2.從運行結果可以看到setup_module和teardown_module只執行了一次
test_fixt.py setup_module:整個.py模塊只執行一次 比如:所有用例開始前只打開一次瀏覽器 setup_function:每個用例開始前都會執行 正在執行----test_one .teardown_function:每個用例結束前都會執行 setup_function:每個用例開始前都會執行 正在執行----test_two Fteardown_function:每個用例結束前都會執行 setup_function:每個用例開始前都會執行 正在執行----test_three .teardown_function:每個用例結束前都會執行 teardown_module:整個.py模塊只執行一次 比如:所有用例結束只最好關閉瀏覽器
備注:setup_function/teardown_function和setup_module/teardown_module這四種方法是可以任意組合的,用一個和多個都可以
類和方法
1.setup/teardown和unittest里面的setup/teardown是一樣的功能,setup_class和teardown_class等價於unittest里面的setupClass和teardownClass
#test_fixtclass.py # coding:utf-8 import pytest # 類和方法 ** 作者:上海-悠悠 QQ交流群:588402570** class TestCase(): def setup(self): print("setup: 每個用例開始前執行") def teardown(self): print("teardown: 每個用例結束后執行") def setup_class(self): print("setup_class:所有用例執行之前") def teardown_class(self): print("teardown_class:所有用例執行之前") def setup_method(self): print("setup_method: 每個用例開始前執行") def teardown_method(self): print("teardown_method: 每個用例結束后執行") def test_one(self): print("正在執行----test_one") x = "this" assert 'h' in x def test_two(self): print("正在執行----test_two") x = "hello" assert hasattr(x, 'check') def test_three(self): print("正在執行----test_three") a = "hello" b = "hello world" assert a in b if __name__ == "__main__": pytest.main(["-s", "test_fixtclass.py"])
運行結果
test_fixtclass.py setup_class:所有用例執行之前
setup_method: 每個用例開始前執行 setup: 每個用例開始前執行 正在執行----test_one .teardown: 每個用例結束后執行 teardown_method: 每個用例結束后執行 setup_method: 每個用例開始前執行 setup: 每個用例開始前執行 正在執行----test_two Fteardown: 每個用例結束后執行 teardown_method: 每個用例結束后執行 setup_method: 每個用例開始前執行 setup: 每個用例開始前執行 正在執行----test_three .teardown: 每個用例結束后執行 teardown_method: 每個用例結束后執行 teardown_class:所有用例執行之前
2.從結果看出,運行的優先級:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
備注:這里setup_method和teardown_method的功能和setup/teardown功能是一樣的,一般二者用其中一個即可
函數和類混合
1.如果一個.py的文件里面既有函數用例又有類和方法用例,運行順序又是怎樣的呢?
# coding:utf-8 import pytest # 類和方法 ** 作者:上海-悠悠 QQ交流群:588402570** def setup_module(): print("setup_module:整個.py模塊只執行一次") print("比如:所有用例開始前只打開一次瀏覽器") def teardown_module(): print("teardown_module:整個.py模塊只執行一次") print("比如:所有用例結束只最后關閉瀏覽器") def setup_function(): print("setup_function:每個用例開始前都會執行") def teardown_function(): print("teardown_function:每個用例結束前都會執行") def test_one(): print("正在執行----test_one") x = "this" assert 'h' in x def test_two(): print("正在執行----test_two") x = "hello" assert hasattr(x, 'check') class TestCase(): def setup_class(self): print("setup_class:所有用例執行之前") def teardown_class(self): print("teardown_class:所有用例執行之前") def test_three(self): print("正在執行----test_three") x = "this" assert 'h' in x def test_four(self): print("正在執行----test_four") x = "hello" assert hasattr(x, 'check') if __name__ == "__main__": pytest.main(["-s", "test_fixtclass.py"])
運行結果:
test_fixtclass.py setup_module:整個.py模塊只執行一次
比如:所有用例開始前只打開一次瀏覽器
setup_function:每個用例開始前都會執行
正在執行----test_one .teardown_function:每個用例結束前都會執行 setup_function:每個用例開始前都會執行 正在執行----test_two Fteardown_function:每個用例結束前都會執行 setup_class:所有用例執行之前 正在執行----test_three .正在執行----test_four Fteardown_class:所有用例執行之前 teardown_module:整個.py模塊只執行一次 比如:所有用例結束只最后關閉瀏覽器
2.從運行結果看出,setup_module/teardown_module的優先級是最大的,然后函數里面用到的setup_function/teardown_function與類里面的setup_class/teardown_class互不干涉