1. 簡介
學過unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例開始前和結束后都去執行一次。
當然還有更高級一點的setupClass和teardownClass,需配合@classmethod裝飾器一起使用,在做selenium自動化的時候,它的效率尤為突出,可以只啟動一次瀏覽器執行多個用例。
pytest框架也有類似于setup和teardown的語法,并且還不止這四個
2. 用例運行級別
模塊級(setup_module/teardown_module)開始于模塊始末,全局的
函數級(setup_function/teardown_function)只對函數用例生效(不在類中)
類級(setup_class/teardown_class)只在類中前后運行一次(在類中)
方法級(setup_method/teardown_method)開始于方法始末(在類中)
類里面的(setup/teardown)運行在調用方法的前后
3. 函數式
3.1 setup_function/teardown_function (每個用例開始和結束時調用一次)
3.1.1 代碼實現:
3.1.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行
# 1.導入模塊
# content of test_bjhg_class1.py
import pytest
# 函數式def setup_function():print("setup_function:每個用例開始前都會執行")def teardown_function():print("teardown_function:每個用例結束后都會執行")def test_one():print("正在執行----test_one")x = "this"assert 'h' in xdef 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 bif __name__ == "__main__":pytest.main(["-s", "test_fixt.py"])
3.1.3運行結果:
運行代碼后,控制臺打印如下圖的結果
3.2 setup_module/teardown_module(所有用例開始和結束時調用一次)
3.2.1 代碼實現:
3.2.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行
# 1.導入模塊
# content of test_class2.py
import pytest
# 函數式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 xdef 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 bif __name__ == "__main__":pytest.main(["-s", "test_class2.py"])
3.2.3 運行結果:
運行代碼后,控制臺打印如下圖的結果
3.3 類和方法
1.setup/teardown和unittest里面的setup/teardown是一樣的功能,setup_class和teardown_class等價于unittest里面的setupClass和teardownClass
3.3.1 代碼實現:
3.3.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行
# 1.導入模塊
# content of test_class3.py
import pytest
# 類和方法
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 xdef 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 bif __name__ == "__main__":pytest.main(["-s", "test_class3.py"])
3.3.3 運行結果:
運行代碼后,控制臺打印如下圖的結果
備注:這里setup_method和teardown_method的功能和setup/teardown功能是一樣的,一般二者用其中一個即可
3.4 函數和類混合
1.如果一個.py的文件里面既有函數用例又有類和方法用例,運行順序又是怎樣的呢?
3.4.1 代碼實現:
3.4.2 參考代碼:
# coding=utf-8
# 1.導入模塊
# content of test_bjhg_class1.py
import pytest
# 類和方法
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 xdef 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 xdef test_four(self):print("正在執行----test_four")x = "hello"assert hasattr(x, 'check')if __name__ == "__main__":pytest.main(["-s", "test_bjhg_class1.py"])
3.4.3 運行結果:
運行代碼后,控制臺打印如下圖的結果
2.從運行結果看出,setup_module/teardown_module的優先級是最大的,然后函數里面用到的setup_function/teardown_function與類里面的setup_class/teardown_class互不干涉