1. 程式人生 > >【Python】Python Assert 為何不盡如人意

【Python】Python Assert 為何不盡如人意

Python中的斷言用起來非常簡單,你可以在assert後面跟上任意判斷條件,如果斷言失敗則會丟擲異常。

>>> assert 1 + 1 == 2
>>> assert isinstance('Hello', str)
>>> assert isinstance('Hello', int)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AssertionError

其實assert看上去不錯,然而用起來並不爽。就比如有人告訴你程式錯了,但是不告訴哪裡錯了。很多時候這樣的assert

還不如不寫,寫了我就想罵娘。直接拋一個異常來得更痛快一些。

改進方案 #1

一個稍微改進一丟丟的方案就是把必要的資訊也放到assert語句後面,比如這樣。

>>> s = "nothin is impossible."
>>> key = "nothing"
>>> assert key in s, "Key: '{}' is not in Target: '{}'".format(key, s)

Traceback (most recent call last):
  File "<input>", line 1, in
<module> AssertionError: Key: 'nothing' is not in Target: 'nothin is impossible.'

看上去還行吧,但是其實寫的很蛋疼。假如你是一名測試汪,有成千上萬的測試案例需要做斷言做驗證,相信你面對以上做法,心中一定有千萬只那種馬奔騰而過。

改進方案 #2

不管你是你是搞測試還是開發的,想必聽過不少測試框架。你猜到我要說什麼了吧?對,不用測試框架裡的斷言機制,你是不是灑。

py.test

py.test 是一個輕量級的測試框架,所以它壓根就沒寫自己的斷言系統,但是它對Python自帶的斷言做了強化處理,如果斷言失敗,那麼框架本身會盡可能多地提供斷言失敗的原因。那麼也就意味著,用py.test

實現測試,你一行程式碼都不用改。

import pytest

def test_case():
    expected = "Hello"
    actual = "hello"
    assert expected == actual

if __name__ == '__main__':
    pytest.main()

"""
================================== FAILURES ===================================
__________________________________ test_case __________________________________

    def test_case():
        expected = "Hello"
        actual = "hello"
>       assert expected == actual
E       assert 'Hello' == 'hello'
E         - Hello
E         ? ^
E         + hello
E         ? ^

assertion_in_python.py:7: AssertionError
========================== 1 failed in 0.05 seconds ===========================
""""

unittest

Python自帶的unittest單元測試框架就有了自己的斷言方法self.assertXXX(),而且不推薦使用assert XXX語句。

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FoO')

if __name__ == '__main__':
    unittest.main()
    
"""
Failure
Expected :'FOO'
Actual   :'FoO'

Traceback (most recent call last):
  File "assertion_in_python.py", line 6, in test_upper
    self.assertEqual('foo'.upper(), 'FoO')
AssertionError: 'FOO' != 'FoO'
"""

ptest

我非常喜歡ptest,感謝Karl大神寫了這麼一個測試框架。ptest中的斷言可讀性很好,而且智慧提示也很方便你通過IDE輕鬆完成各種斷言語句。

from ptest.decorator import *
from ptest.assertion import *

@TestClass()
class TestCases:
    @Test()
    def test1(self):
        actual = 'foo'
        expected = 'bar'
        assert_that(expected).is_equal_to(actual)

"""
Start to run following 1 tests:
------------------------------
...
[[email protected]] Failed with following message:
...
AssertionError: Unexpectedly that the str <bar> is not equal to str <foo>.
"""

改進方案 #3

不僅僅是你和我對Python中的斷言表示不滿足,所以大家都爭相發明自己的assert包。在這裡我強烈推薦assertpy 這個包,它異常強大而且好評如潮。

pip install assertpy

看例子:

from assertpy import assert_that

def test_something():
    assert_that(1 + 2).is_equal_to(3)
    assert_that('foobar')\
        .is_length(6)\
        .starts_with('foo')\
        .ends_with('bar')
    assert_that(['a', 'b', 'c'])\
        .contains('a')\
        .does_not_contain('x')

從它的github 主頁 文件上你會發現它支援了幾乎你能想到的所有測試場景,包括但不限於以下列表。

  • Strings

  • Numbers

  • Lists

  • Tuples

  • Dicts

  • Sets

  • Booleans

  • Dates

  • Files

  • Objects

而且它的斷言資訊簡潔明瞭,不多不少。

Expected <foo> to be of length <4>, but was <3>.
Expected <foo> to be empty string, but was not.
Expected <False>, but was not.
Expected <foo> to contain only digits, but did not.
Expected <123> to contain only alphabetic chars, but did not.
Expected <foo> to contain only uppercase chars, but did not.
Expected <FOO> to contain only lowercase chars, but did not.
Expected <foo> to be equal to <bar>, but was not.
Expected <foo> to be not equal to <foo>, but was.
Expected <foo> to be case-insensitive equal to <BAR>, but was not.

在發現assertpy之前我也想寫一個類似的包,儘可能通用一些。但是現在,我為毛要重新去造輪子?完全沒必要!

總結

斷言在軟體系統中有非常重要的作用,寫的好可以讓你的系統更穩定,也可以讓你有更多真正面對物件的時間,而不是在除錯程式碼。

Python中預設的斷言語句其實還有一個作用,如果你寫了一個型別相關的斷言,IDE會把這個物件當成這種型別,這時候智慧提示就有如神助。

要不要把內建的斷言語句換成可讀性更好功能更強大的第三方斷言,完全取決於實際情況。比如你真的需要驗證某個東西並且很關心驗證結果,那麼必須不能用簡單的assert;如果你只是擔心某個點可能有坑或者讓IDE認識某個物件,用內建的assert既簡單又方便。

所以說,專案經驗還是蠻重要的。

相關推薦

PythonPython Assert 何不盡如人意

Python中的斷言用起來非常簡單,你可以在assert後面跟上任意判斷條件,如果斷言失敗則會丟擲異常。 >>> assert 1 + 1 == 2 >>> assert isinstance('Hello', str) >>

Pythonxpath中什麽粘貼進去代碼後老報錯?如何在定位元素的時候準確找到定位切入點?

ctrl+ 試用 不能 -s 如何 ont mage F12 ctr 1. xpath後()中雙引號("")裏面不能套用雙引號(""),把裏面的雙引號改成單引號(‘‘)報錯就沒有了。 2.如何在定位元素的時候準確找到定位切入點? 要巧妙運用F12,確定要定位的頁面元素

轉+python什麽推薦使用多進程

文件處理 自動切換 問題 決定 其他 意義 一個 strong 計算 最近在看Python的多線程,經常我們會聽到老手說:“Python下多線程是雞肋,推薦使用多進程!”,但是為什麽這麽說呢? 要知其然,更要知其所以然。所以有了下面的深入研究:

pythonpython將資料儲存文字檔案的兩種語法

file = open('filename','a',encoding='utf-8') file.write('......') file.close 上面這種標準的檔案儲存方式,開啟檔案、寫入資料、關閉檔案。 下面這種寫法,會隨著with語句的結束,自動關閉。 wi

原創Python 對象創建過程中元類, __new__, __call__, __init__ 的處理

diff regular luci 自定義 weight ica 一般來說 att ray 原始type: type是最原始的元類,其__call__方法是在你使用" t_class = type(classname_string, base_classes_tuple,

NLPPython實例:基於文本相似度對申報項目進行查重設計

用戶 strip() 字符串 執行 原創 這樣的 string 得到 亂碼問題 Python實例:申報項目查重系統設計與實現 作者:白寧超 2017年5月18日17:51:37 摘要:關於查重系統很多人並不陌生,無論本科還是碩博畢業都不可避免涉及論文查重問題,這也

Python自動化開發課堂筆記Day03 - Python基礎(字符編碼使用,文件處理,函數)

賦值 創建 解釋器 使用 重復 closed 操作 邏輯 默認 字符編碼使用 1. 文本編輯器如何存取文件 文本編輯器相當一個運行在內存中的進程,所以文件內容在編輯未存儲時都是在內存中的,尚未存儲在硬盤之中,在沒有保存之前,所編輯的任何文本都只是一堆字符,沒有任何邏輯上的意

pythonpython魔法方法(待填坑)

絕對值 tle init cls -m del __init__ 另一個 trunc 參考博文:http://pyzh.readthedocs.io/en/latest/python-magic-methods-guide.html 參考博文英文原版:http://www

zhlan--Python中的賦值運算符

運算 alt ges 比較 images pytho 比較運算符 賦值 技術分享 >>>>Python中的賦值運算符: >>>>Python中的比較運算符: zhlan--【偷】Python中的賦值運算符

Python自動化開發課堂筆記Day06 - Python進階(類)

擴展性 程序 lex 類名 人物 優點 ini 參數 self. 類與對象 面向過程的程序設計:  優點:極大的降低了程序的復雜度  缺點:一套流水線或者流程就是用來解決一個問題,生產汽水的流水線無法生產汽車,即使能,也是得大改,改一個組件,牽一發而動全身面向對象的程序設計

Python自動化開發課堂筆記Day08 - Python進階(面向對象的高級用法,網絡編程)

sta 自然 log 報錯 面向 read urn total 析構函數 面向對象的高級用法 1. __str__ 只要執行打印對象的操作,就會觸發該對象類中的__str__方法(也就是對象的綁定方法)它是一種默認的方法,默認的打印輸出為<__main__.Foo o

PythonPython基礎

pytho 程序 填充 type() 操作 bsp 方式 num 禁止 源程序文件通常以.py為擴展名 #!/usr/bin/python shebang,即執行腳本時通知內容要啟動的解釋器 impor

python qt(pyqt)的文件打開、文件保存、文件夾選擇對話框

utf spl 文件對話框 出現 tin ans none 轉換 選擇文件夾 import PyQt4.QtCore,PyQt4.QtGui # 獲取文件路徑對話框 file_name = QFileDialog.getOpenFileName(self,"open

Pythonpython動態類型

引用變量 區分 如何工作 回收 new images 如何 空間 簡單的 在python中,省去了變量聲明的過程,在引用變量時,往往一個簡單的賦值語句就同時完成了,聲明變量類型,變量定義和關聯的過程,那麽python的變量到底是怎樣完成定義的呢? 動態類型  

pythonpython版本升級2.6.6到2.7.3(CentOS release 6.2)

configure pac packages 應該 zxvf 修改 figure oca nbsp 一、 升級python到2.7.3 wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz tar -z

pythonpython獲取當前日期前後N天或N月的日期

color ont mes form localtime col r+ arr nth 1 # -*- coding: utf-8 -*- 2 3 ‘‘‘獲取當前日期前後N天或N月的日期‘‘‘ 4 5 from time import strfti

python字符串/元組/列表/字典互轉

python字符串 互轉 utf () __str__ 返回 list log utf-8 #-*-coding:utf-8-*- #1、字典 dict = {‘name‘: ‘Zara‘, ‘age‘: 7, ‘class‘: ‘First‘} #字典轉為字符串,

Pythonpython基礎語法 編碼

finall ont 實現 tro out 程序 port 其他 pytho 編碼 默認情況下,python以UTF-8編碼,所有的字符串都是Unicode字符串,可以為代碼定義不同的的編碼。 #coding:UTF-8 #OR #-*- coding:UTF-8 -

Python+pyplot繪制帶文本標註的柱狀圖

spa app topic tro ins str com png 技術分享 import numpy as npimport matplotlib.pyplot as plt # 生成測試數據x = np.linspace(0, 10, 11)y = 11-x #

python的淺拷貝和深拷貝

end ont code append pen ron 實例 賦值 深拷貝 直接賦值:其實就是對象的引用(別名)。 淺拷貝(copy):拷貝父對象,不會拷貝對象的內部的子對象。 深拷貝(deepcopy): copy 模塊的 deepcopy 方法,完全拷貝了父對象及其