Pytest跳過執行之@pytest.mark.skip()詳解

  • 2019 年 12 月 15 日
  • 筆記

一、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==1      def test_two(self):          print('test_02')          assert 1==2  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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==1      def test_two(self):          print('test_02')          assert 1==2  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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 0    2、跳過方法或用例,備註了原因  import pytest,sys  class Test(object):      @pytest.mark.skip(reason='跳過一個方法或一個測試用例')      def test_one(self):          assert 1==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.py s.                                                             [100%]    =========================== short test summary info ===========================  SKIPPED [1] test01.py:414: 跳過一個方法或一個測試用例  ======================== 1 passed, 1 skipped in 0.05s =========================    Process finished with exit code 0    3、當條件滿足,跳過方法或用例,備註了原因  import pytest,sys  class Test(object):      @pytest.mark.skipif(1==1,reason='跳過一個方法或一個測試用例')      def test_one(self):          assert 1==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])    "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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):      @myskip      def test_one(self):          assert 1==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.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==2      def test_two(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-rs','test01.py'])      "C:Program FilesPython35python.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:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.py s.                                                             [100%]    =========================== short test summary info ===========================  SKIPPED [1] c:userswanglipycharmprojectstesttesttest01.py:416: 跳過  ======================== 1 passed, 1 skipped in 0.04s =========================    Process finished with exit code 0

Tags: