簡介
??????前面使用過unittest的都知道,setup和teardown是用來處理用例的前置工作和清理工作,其中還有setupclass和teardownclass是保證執行所以的用例都只執行1次前置和后置,使用起來非常方便,對于強大的pytest測試框框,給我們提供了10中類似方法。
- 模塊級別:setup_module、teardown_module
- 函數級別:setup_function、teardown_function
- 類級別:setup_class、teardown_class
- 類方法級別:setup_method、teardown_method
- 函數和方法級別:setup、teardown
詳細用法講解
模塊級別:setup_module、teardown_module
說明:該方法表示只能類外面執行用例過程中,只執行1次。相當于unittest中的setupclass和teardownclass方法
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def setup_module():
print('這是setup_module的前置')
def teardown_module():
print('這是setup_module的后置')
def test_01():
print('我是test_01用例')
class TestDemo:
def test_one(self):
print("我是類里面方法 test_one")
def test_two(self):
print("我是類里面方法 test_two")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
函數級別:setup_function、teardown_function
說明:該方法表示在類外面執行用例過程中,每次都會執行前置和后置。
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def setup_function():
print('這是 setup_function 的前置')
def teardown_function():
print('這是 teardown_function 的后置')
def test_01():
print('我是類外面的 test_01 用例')
class TestDemo:
def test_one(self):
print("我是類里面方法 test_one")
def test_two(self):
print("我是類里面方法 test_two")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
類級別:setup_class、teardown_class
說明:該方法表示在類中執行測試用例前,只執行1次測試前置和測試后置,注意:<font color=red>放在類外面不生效</font>
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def test_01():
print('我是類外面的 test_01 用例')
class TestDemo:
def setup_class(self):
print("我是 setup_class 方法")
def teardown_class(self):
print("我是 teardown_class 方法")
def test_one(self):
print("我是類里面方法 test_one")
def test_two(self):
print("我是類里面方法 test_two")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
方法級別:setup_method、teardown_method
說明:該方法表示在類中每次執行測試用例前,測試前置和測試后置都會執行一次,注意:<font color=red>放在類外面不生效</font>
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def test_01():
print('我是類外面的 test_01 用例')
class TestDemo:
def setup_method(self):
print("我是 setup_method 方法")
def teardown_method(self):
print("我是 teardown_method 方法")
def test_one(self):
print("我是類里面方法 test_one")
def test_two(self):
print("我是類里面方法 test_two")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
函數和方法級別:setup、teardown
說明:該方法這個可以在類中使用,也可以在類外進行使用,大家最熟悉
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def setup():
print("我是類外面 setup 方法")
def teardown():
print("我是類外面 teardown 方法")
def test_01():
print('我是類外面的 test_01 用例')
class TestDemo:
def setup(self):
print("我是類里面 setup 方法")
def teardown(self):
print("我是類里面 teardown 方法")
def test_one(self):
print("我是類里面方法 test_one")
def test_two(self):
print("我是類里面方法 test_two")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
多個方法組合使用
說明:直接查看示例,<font color=red>查看各個方法執行優先級,非常重要</font>
示例如下:
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【測試開發知識庫】
import pytest
def setup_module():
print("我是類外面的 setup_module 方法")
def teardown_module():
print("我是類外面的 teardown_module 方法")
def setup_function():
print("我是類外面的 setup_function 方法")
def teardown_function():
print("我是類外面的 teardown_function 方法")
def setup():
print("我是類外面 setup 方法")
def teardown():
print("我是類外面 teardown 方法")
def test_one():
print("我是類外面的 test_one 用例")
def test_two():
print("我是類外面的 test_two 用例")
class TestDemo():
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 setup(self):
print("我是類里面 setup 方法")
def teardown(self):
print("我是類里面 teardown 方法")
def test_one(self):
print("我是類里面的 test_one 用例")
def test_two(self):
print("我是類里面的 test_two 用例")
if __name__ == '__main__':
pytest.main()
命令行輸入: pytest -s test_demo01.py
執行結果如下:
我們根據執行結果看見方法執行順序
- setup_module、teardown_module 整個測試只執行一次
- setup_function、teardown_function 類外面方法每個用例都執行
- setup_class、teardown_class 類里面只執行一次
- setup_method、teardown_method 類里面每個用例都執行
- setup、teardown 類里面和外面每個用例都執行
優先級為:
- setup_module>setup_function>setup>用例>teardown>teardown_function>teardown_module
- setup_module>setup_class>setup_method>setup>用例>teardown>teardown_method>teardown_class>teardown_module
歡迎您給我留言,我們一起討論。