簡體   English   中英

python中的輕量級單元測試

[英]lightweight unit testing in python

我正在考慮使用Python來教授入門編程,我正在尋找一個輕量級的單元測試框架。 我已經看到了在單元測試,以及-據我可以告訴-它看起來出奇的 -lightweight。

例如,這是我想要寫的內容:

import unittest

def f(x):
  return x+2

checkEqual(f(3),5)

...... 別無其他 為了告訴你我來自哪里,這就是我用Racket開始的學生語言寫的:

(define (f x)
  (+ x 2))

(check-expect (f 3) 5)

......就是這樣。 當然有人寫了這個工具,我只是沒找到它?

(對任何火焰誘餌的出現都要提前道歉。這是一個嚴肅的問題。)

自編輯:

在任何人指出這一點之前:是的,我可以寫def checkEqual(a,b):print(a == b); 我正在尋找更多的東西:它應該能夠檢查帶有容差的數字,它應該支持僅打印失敗的測試用例,它應該能夠告訴你有多少測試用例失敗了。 再次,我相信這段代碼可以寫出來; 我只是想避免重新發明輪子。

我會推薦Doctest

你的例子看起來像:

def f(x):
    """
    >>> f(3)
    5
    """
    return x + 2

為什么?:

  1. 它非常簡單:“當我運行這個東西時,我應該得到這個回答”
  2. 它適用於功能級別 - 這可能允許您甚至在課前引入測試
  3. 鏡像交互式Python體驗。

Doctests是一個很好的建議,但是如果你想接近你的示例代碼我會建議py.test(pytest.org)。 你的例子將被寫成:

def f(x):
    return x+2

def test_equal():       # py.test looks for functions that start with test_
    assert f(3) == 5

如果我把它放在一個名為tt.py的文件中並使用py.test運行它,它看起來像這樣:

w:\tmp>py.test tt.py
============================= test session starts =============================
platform win32 -- Python 2.6.6 -- pytest-2.2.3
collected 1 items

tt.py .

========================== 1 passed in 0.01 seconds ===========================

如果我將斷言更改為f(3)== 6,並再次運行它我得到:

w:\tmp>py.test tt.py
============================= test session starts =============================
platform win32 -- Python 2.6.6 -- pytest-2.2.3
collected 1 items

tt.py F

================================== FAILURES ===================================
_________________________________ test_equal __________________________________

    def test_equal():       # py.test looks for functions that start with test_
>       assert f(3) == 6
E       assert 5 == 6
E        +  where 5 = f(3)

tt.py:5: AssertionError
========================== 1 failed in 0.01 seconds ===========================

py.test也可以擴展,你可以讓它運行覆蓋,在多個CPU上分配測試等。它還可以找到並運行unittest測試,也可以運行doctests。

兩種常見的替代方案是nosepy.test 它們都具有非常輕量級的語法,但功能全面。

這是對鼻子的擴展介紹: http//ivory.idyll.org/articles/nose-intro.html

這是一個使用py.test的示例函數和測試:

# content of test_sample.py
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

從命令行運行測試:

$ py.test
=========================== test session starts ============================
platform darwin -- Python 2.7.1 -- pytest-2.2.2
collecting ... collected 1 items

test_sample.py F

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
========================= 1 failed in 0.02 seconds =========================
import unittest

def f(x):
    return x + 2

class TestFunctionF(unittest.TestCase):
    def test_f(self):
        self.assertEqual(f(3), 5)

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

生產:

----------------------------------------------------------------------
Ran 1 test in 0.000s
OK

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM