Python單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過和預(yù)期故障的方法
Python單元測(cè)試unittest中提供了一下四種裝飾器實(shí)現(xiàn)測(cè)試跳過和預(yù)期故障。(使用Python 2.7.13)
請(qǐng)查考Python手冊(cè)中:
https://docs.python.org/dev/library/unittest.html
The following decorators implement test skipping and expected failures:
#以下裝飾器實(shí)施測(cè)試跳過和預(yù)期故障:
@unittest.skip(原因)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
#無(wú)條件跳過裝飾測(cè)試。 原因應(yīng)該說明為什么要跳過測(cè)試。
@unittest.skipIf(條件,原因)
Skip the decorated test if condition is true.
#如果條件為真,跳過裝飾測(cè)試。
@unittest.skipUnless(條件,原因)
Skip the decorated test unless condition is true.
# 跳過裝飾的測(cè)試,除非條件是真的。
@unittest.expectedFailure
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
#將測(cè)試標(biāo)記為預(yù)期的失敗。 如果測(cè)試在運(yùn)行時(shí)失敗,則測(cè)試不會(huì)被視為失敗。
(以上采用谷歌翻譯,可能會(huì)有差異)
好了,寫段代碼看下,test.py ,使用的Eclipse
#coding:UTF-8
import unittest
from test.test_pprint import uni
class Test_ce(unittest.TestCase):
a=16
b=10
@unittest.skip('無(wú)條件跳過')
def test_ce1(self):
self.assertEqual((self.a-self.b), 16)
#判斷是否相等
@unittest.skipIf(True==1, '條件為真則跳過')
def test_ce_2(self):
self.assertFalse(self.a==self.b)
#判斷是否為False
@unittest.skipUnless(1==1, '條件為假則跳過')
def test_ce_3(self):
self.assertTrue(self.a>16)
#判斷是否為True
@unittest.expectedFailure
def test_ce_4(self):
self.assertFalse(self.a==16)
@unittest.expectedFailure
def test_ce_5(self):
self.assertFalse(self.a==15)
if __name__ == '__main__':
unittest.main()
好的,運(yùn)行一下
ssFxu ====================================================================== FAIL: test_ce_3 (__main__.Test_ce) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\Escplise\workspace\Pytest\src\test001\CE.py", line 20, in test_ce_3 self.assertTrue(self.a>16) AssertionError: False is not true ---------------------------------------------------------------------- Ran 5 tests in 0.000s FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
好的,我們對(duì)第1行代碼進(jìn)行分析:
s:全稱是skipped(跳過)
s:條件為真,所以也是skipped(跳過)
F:條件為真,所以忽略裝飾器,執(zhí)行斷言代碼,顯然是failures(失敗)
x:斷言結(jié)果顯然是失敗的,但是這是在我們意料之中,所以是expected failures(預(yù)期的失?。?/p>
u:斷言結(jié)果顯然是pass,但是我們預(yù)計(jì)可能不通過,所以是unexpected successes(意想不到的成功)
即第13行代碼 所示 FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
以上這篇Python單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過和預(yù)期故障的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python單元測(cè)試unittest實(shí)例詳解
- Python單元測(cè)試框架unittest使用方法講解
- 在Python中進(jìn)行自動(dòng)化單元測(cè)試的教程
- 在Python的Flask框架中實(shí)現(xiàn)單元測(cè)試的教程
- Python之PyUnit單元測(cè)試實(shí)例
- 對(duì)Python的Django框架中的項(xiàng)目進(jìn)行單元測(cè)試的方法
- Python中unittest模塊做UT(單元測(cè)試)使用實(shí)例
- 在Python編程過程中用單元測(cè)試法調(diào)試代碼的介紹
- 詳解Python nose單元測(cè)試框架的安裝與使用
- 深入理解Python單元測(cè)試unittest的使用示例
- Python Unittest自動(dòng)化單元測(cè)試框架詳解
- Python單元測(cè)試實(shí)例詳解
相關(guān)文章
java指紋識(shí)別以及谷歌圖片識(shí)別技術(shù)源碼
這篇文章主要為大家詳細(xì)愛介紹了java指紋識(shí)別以及谷歌圖片識(shí)別技術(shù)源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
java數(shù)據(jù)結(jié)構(gòu)和算法之馬踏棋盤算法
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)和算法之馬踏棋盤算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
MyBatis處理mysql主鍵自動(dòng)增長(zhǎng)出現(xiàn)的不連續(xù)問題解決
本文主要介紹了MyBatis處理mysql主鍵自動(dòng)增長(zhǎng)出現(xiàn)的不連續(xù)問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
spring mvc 實(shí)現(xiàn)獲取后端傳遞的值操作示例
這篇文章主要介紹了spring mvc 實(shí)現(xiàn)獲取后端傳遞的值操作,結(jié)合實(shí)例形式詳細(xì)分析了spring mvc使用JSTL 方法獲取后端傳遞的值相關(guān)操作技巧2019-11-11
詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java8時(shí)間接口LocalDateTime詳細(xì)用法
最近看別人項(xiàng)目源碼,發(fā)現(xiàn)Java8新的日期時(shí)間API很方便強(qiáng)大,所以整理了這篇文章,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
使用Java7的Files工具類和Path接口來(lái)訪問文件的方法
下面小編就為大家分享一篇使用Java7的Files工具類和Path接口來(lái)訪問文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-11-11
springboot解決java.lang.ArrayStoreException異常
這篇文章介紹了springboot解決java.lang.ArrayStoreException異常的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12

