tft每日頭條

 > 生活

 > py test參數化

py test參數化

生活 更新时间:2025-02-07 17:07:34
前言

小夥伴們好呀,我又來了我們今天聊聊關于pytest前後置應用,使用過unittest的小夥伴們都知道,setup和teardown是用來處理用例的開始前工作和結束後的工作,其中還有setupclass和teardownclass是保證執行所以的用例都隻執行1次前置和後置,使用起來非常方便,那麼學習pytest強大的測試框框,肯定也有這個功能,并且還比unittest的簡單不少。

py test參數化(pytestsetup和teardown簡單用法)1

pytest中的前置

pytest比較強大,提供了不僅僅一種方法的前置和後置:

  • setup_module、teardown_module
  • setup_function、teardown_function
  • setup_class、teardown_class
  • setup_method、teardown_method
  • setup、teardown

光看上面的内容,肯定一臉懵逼,不知道到底什麼時候使用,我來通過舉例一個個介紹

setup、teardown

先介紹第一個大家都比較熟悉的與unittest中的書寫一緻,這個可以在類中使用,也可以在類外進行使用。

該方法每條用例都會執行

import pytest def setup(): print('這是測試用例的前置') def teardown(): print('這是測試用例的後置') def test01(): print('用例01') def test02(): print('用例02') if __name__ == '__main__': pytest.main(['-s'])

py test參數化(pytestsetup和teardown簡單用法)2

setup_module、teardown_module

該方法表示隻能類外面執行用例過程中,隻執行1次。相當于unittest中的setupclass和teardownclass方法

import pytest def setup_module(): print('這是測試用例的前置') def teardown_module(): print('這是測試用例的後置') def test01(): print('用例01') def test02(): print('用例02') if __name__ == '__main__': pytest.main(['-s','test_02.py'])

py test參數化(pytestsetup和teardown簡單用法)3

setup_function、teardown_function

該方法表示在類外面執行用例過程中,每次都會執行前置和後置。

import pytest def setup_function(): print('這是測試用例的前置') def teardown_function(): print('這是測試用例的後置') def test01(): print('用例01') def test02(): print('用例02') if __name__ == '__main__': pytest.main(['-s','test_02.py'])

py test參數化(pytestsetup和teardown簡單用法)4

setup_method、teardown_method

該方法表示在類中每次執行測試用例前,測試前置和測試後置都會執行一次

# coding:utf-8 import pytest class Test(): def setup_method(self): print('這是setup函數前置内容') def teardown_method(self): print('這是teardown函數後置内容') def test01(self): print('這是測試用例1') def test02(self): print('這是測試用例2') if __name__ == '__main__': pytest.main(['-s','test_01.py'])

py test參數化(pytestsetup和teardown簡單用法)5

setup_class、teardown_class

該方法表示在類中執行測試用例前,隻執行1次測試前置和測試後置

# coding:utf-8 import pytest class Test(): def setup_class(self): print('這是setup函數前置内容') def teardown_class(self): print('這是teardown函數後置内容') def test01(self): print('這是測試用例1') def test02(self): print('這是測試用例2') if __name__ == '__main__': pytest.main(['-s','test_01.py'])

py test參數化(pytestsetup和teardown簡單用法)6

組合混用

上面介紹了每種方法的單獨用法,那麼如果這些方法都一起使用?會怎麼樣?

setup_class和setup_method、setup混合

# coding:utf-8 import pytest class Test(): def setup_method(self): print('這是setup_method用例前置内容') def setup_class(self): print('這是setup_class用例前置内容') def setup(self): print('這是setup用例前置内容') def teardown_class(self): print('這是teardown_class用例後置内容') def teardown_method(self): print('這是teardown_method用例後置内容') def teardown(self): print('這是teardown用例後置内容') def test01(self): print('這是測試用例1') def test02(self): print('這是測試用例2') if __name__ == '__main__': pytest.main(['-s','test_01.py'])

py test參數化(pytestsetup和teardown簡單用法)7

通過上面測試結果可以看出來,其中執行順序:setup_class>>setup_method>>setup,其中setup和setup_method都是表示執行每條用例前都需要進行執行前置内容

setup_module、setup_function組合

import pytest def setup(): print('這是setup測試用例前置内容') def setup_function(): print('這是setup_function測試用例前置内容') def setup_module(): print('這是setup_module測試用例前置内容') def teardown_module(): print('這是teardown_module測試用例後置内容') def teardown(): print('這是teardown測試用例後置内容') def teardown_function(): print('這是teardown_function測試用例後置内容') def test01(): print('用例01') def test02(): print('用例02') if __name__ == '__main__': pytest.main(['-s','test_02.py'])

py test參數化(pytestsetup和teardown簡單用法)8

通過上述測試發現,執行順序:setup_module>>setup_function>>setup。其中setup_module表示執行用例隻執行一次前置。

總結:

1、setup_class和setup_module執行用例時,隻執行一次前置和後置

2、setup_class,setup_method,setup是在類中執行的

3、setup_module,setup_function,setup是在類外執行的

4、其中setup類中,類外都可以執行。

阿六通過簡單的例子介紹了pytest的setup和teardown的使用方法和類型,希望大家能夠自己行動起來喲,畢竟實踐是真理。

如果大家喜歡這篇文章可以動動發财的小手給阿六點個贊和收藏,順便加個關注,關注阿六不迷路喲。

py test參數化(pytestsetup和teardown簡單用法)9

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved