日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pytest跳过执行之@pytest.mark.skip()详解大全

發布時間:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytest跳过执行之@pytest.mark.skip()详解大全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一、skip介紹及運用

? ? ? 在我們自動化測試過程中,經常會遇到功能阻塞、功能未實現、環境等一系列外部因素問題導致的一些用例執行不了,這時我們就可以用到跳過skip用例,如果我們注釋掉或刪除掉,后面還要進行恢復操作。

1、skip跳過成功,標識為s??============================= 2 skipped in 0.04s ==============================

2、pytest.main(['-rs','test01.py']) 用-rs執行,跳過原因才會顯示SKIPPED [1] test01.py:415: 跳過Test類,會跳過類中所有方法

3、skip跳過,無條件和原因@pytest.mark.skipif()

4、skip跳過,無需滿足條件true、有跳過原因@pytest.mark.skipif(reason='無條件,只有跳過原因')

5、skip跳過,需滿足條件true、且有跳過原因@pytest.mark.skipif(條件1==1,reason='跳過原因')

6、skip賦值變量,多處調用myskip=pytest.mark.skipif(1==1,reason='skip賦值給變量,可多處調用')

然后@myskip使用

二、跳過測試類

@pytest.mark.skip()和@pytest.mark.skipif()兩個標簽,用他們裝飾測試類

1、被標記的類中所有方法測試用例都會被跳過

2、被標記的類,當條件為ture時,會被跳過,否則不跳過

#skip跳過類import pytest,sys @pytest.mark.skip(reason='跳過Test類,會跳過類中所有方法') class Test(object):def test_one(self):assert 1==1def test_two(self):print('test_02')assert 1==2 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py ss [100%]=========================== short test summary info =========================== SKIPPED [2] test01.py: 跳過Test類,會跳過類中所有方法 ============================= 2 skipped in 0.07s ==============================Process finished with exit code 0#skip滿足條件,skip跳過類 import pytest,sys @pytest.mark.skipif(1==1,reason='跳過Test類,會跳過類中所有方法') class Test(object):def test_one(self):assert 1==1def test_two(self):print('test_02')assert 1==2 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py ss [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:415: 跳過Test類,會跳過類中所有方法 SKIPPED [1] test01.py:417: 跳過Test類,會跳過類中所有方法 ============================= 2 skipped in 0.04s ==============================Process finished with exit code 0

三、跳過方法或測試用例

我們想要某個方法或跳過某條用例,在方法上加以下3種都可以

@pytest.mark.skip() #1、跳過方法或用例,未備注原因

@pytest.mark.skip(reason='跳過一個方法或一個測試用例') #2、跳過方法或用例,備注了原因

@pytest.mark.skipif(1==1,reason='跳過一個方法或一個測試用例')? ?#3、當條件滿足,跳過方法或用例,備注了原因

1、跳過方法或用例,未備注原因 import pytest,sys class Test(object):@pytest.mark.skip()def test_one(self):assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py s. [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:414: unconditional skip ======================== 1 passed, 1 skipped in 0.04s =========================Process finished with exit code 02、跳過方法或用例,備注了原因 import pytest,sys class Test(object):@pytest.mark.skip(reason='跳過一個方法或一個測試用例')def test_one(self):assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py s. [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:414: 跳過一個方法或一個測試用例 ======================== 1 passed, 1 skipped in 0.05s =========================Process finished with exit code 03、當條件滿足,跳過方法或用例,備注了原因 import pytest,sys class Test(object):@pytest.mark.skipif(1==1,reason='跳過一個方法或一個測試用例')def test_one(self):assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py s. [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:414: 跳過一個方法或一個測試用例 ======================== 1 passed, 1 skipped in 0.06s =========================Process finished with exit code 0

四、多個skip時,滿足1個條件即跳過

我們在類和方法上分別加了skip,類中滿足條件,方法中未滿足條件,所以生效類中skip

import pytest,sys @pytest.mark.skipif(1==1,reason='多個條件時,有1個條件滿足就跳過(類)') class Test(object):@pytest.mark.skipif(1==2, reason='多個條件時,有1個條件滿足就跳過(方法)')def test_one(self):assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py ss [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:418: 多個條件時,有1個條件滿足就跳過(類) SKIPPED [1] test01.py:415: 多個條件時,有1個條件滿足就跳過(類) ============================= 2 skipped in 0.04s ==============================

五、skip賦值給變量,可多出調用

無論是@pytest.mark.skip()標簽還是@pytest.mark.skipif()標簽,如果你想在多個測試方法上裝飾,依次寫起來很麻煩的話,你可以選擇定義個變量讓它等于標簽,然后在裝飾的時候用該變量代替標簽。這種方法,你還可以通過在其他模塊中導入的變量的方式,在其他模塊中共享標簽;如果可以這樣的話,我們為什么不新建一個模塊用來存放標簽呢?這樣是不是又方便了許多。

賦值:myskip=pytest.mark.skipif(1==1,reason='skip賦值給變量,可多處調用')

調用:@myskip

import pytest,sys myskip=pytest.mark.skipif(1==1,reason='skip賦值給變量,可多處調用') class Test(object):@myskipdef test_one(self):assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py s. [100%]=========================== short test summary info =========================== SKIPPED [1] test01.py:415: skip賦值給變量,可多處調用 ======================== 1 passed, 1 skipped in 0.07s =========================Process finished with exit code 0

六、pytest.skip()方法內跳過

除了通過使用標簽的方式,還可以在測試用例中調用pytest.skip()方法來實現跳過,可以選擇傳入msg參數來說明跳過原因;如果想要通過判斷是否跳過,可以寫在if判斷里(_)

import pytest,sys class Test(object):def test_one(self):pytest.skip(msg='跳過')assert 1==2def test_two(self):print('test_02')assert 1==1 if __name__=='__main__':pytest.main(['-rs','test01.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0 collected 2 itemstest01.py s. [100%]=========================== short test summary info =========================== SKIPPED [1] c:\users\wangli\pycharmprojects\test\test\test01.py:416: 跳過 ======================== 1 passed, 1 skipped in 0.04s =========================Process finished with exit code 0

?

?

總結

以上是生活随笔為你收集整理的Pytest跳过执行之@pytest.mark.skip()详解大全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。