Python 單元測(cè)試:八個(gè)單元測(cè)試框架的使用方法
單元測(cè)試是軟件開(kāi)發(fā)中不可或缺的一部分,它能夠幫助開(kāi)發(fā)者確保代碼的質(zhì)量和穩(wěn)定性。Python 社區(qū)提供了多種單元測(cè)試框架,每種框架都有其獨(dú)特的優(yōu)勢(shì)和適用場(chǎng)景。本文將介紹幾種常見(jiàn)的 Python 單元測(cè)試框架,并通過(guò)實(shí)際例子幫助讀者更好地理解和使用它們。
1. unittest 模塊
unittest 是 Python 自帶的標(biāo)準(zhǔn)庫(kù)之一,它基于 Java 的 JUnit 框架設(shè)計(jì),提供了一套完整的單元測(cè)試框架。
基本用法:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
if __name__ == '__main__':
unittest.main()
- 這段代碼定義了一個(gè)測(cè)試類 TestStringMethods,繼承自 unittest.TestCase。
- test_upper 和 test_isupper 方法分別測(cè)試字符串的大寫(xiě)轉(zhuǎn)換和是否全為大寫(xiě)的檢查。
- unittest.main() 啟動(dòng)測(cè)試運(yùn)行器。
進(jìn)階用法:
import unittest
class TestStringMethods(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("這個(gè)方法只在所有測(cè)試開(kāi)始前執(zhí)行一次")
def setUp(self):
print("這個(gè)方法會(huì)在每個(gè)測(cè)試方法之前執(zhí)行")
self.test_string = "hello world"
def test_upper(self):
self.assertEqual(self.test_string.upper(), 'HELLO WORLD')
def test_isupper(self):
self.assertTrue('HELLO'.isupper())
self.assertFalse('Hello'.isupper())
def tearDown(self):
print("這個(gè)方法會(huì)在每個(gè)測(cè)試方法之后執(zhí)行")
del self.test_string
@classmethod
def tearDownClass(cls):
print("這個(gè)方法在所有測(cè)試結(jié)束后執(zhí)行一次")
if __name__ == '__main__':
unittest.main()
- setUpClass 類方法在整個(gè)測(cè)試類開(kāi)始前執(zhí)行一次。
- setUp 方法在每個(gè)測(cè)試方法前執(zhí)行,用于準(zhǔn)備測(cè)試數(shù)據(jù)。
- tearDown 方法在每個(gè)測(cè)試方法后執(zhí)行,用于清理測(cè)試環(huán)境。
- tearDownClass 類方法在所有測(cè)試結(jié)束后執(zhí)行一次。
2. pytest 框架
pytest 是目前非常流行的一個(gè)第三方單元測(cè)試框架,它簡(jiǎn)潔易用,擴(kuò)展性強(qiáng)。
基本用法:
def test_upper():
assert 'foo'.upper() == 'FOO'
def test_isupper():
assert 'FOO'.isupper()
assert not 'Foo'.isupper()
- 使用 assert 斷言來(lái)驗(yàn)證期望的結(jié)果。
- 直接定義函數(shù)名以 test_ 開(kāi)頭的方法作為測(cè)試用例。
進(jìn)階用法:
import pytest
@pytest.fixture
def setup_data():
print("setup data")
return "hello world"
def test_upper(setup_data):
assert setup_data.upper() == "HELLO WORLD"
def test_isupper():
assert 'HELLO'.isupper()
assert not 'Hello'.isupper()
def test_fixture_teardown(setup_data):
print("teardown data")
- @pytest.fixture 裝飾器定義了一個(gè)測(cè)試夾具(fixture),可以在多個(gè)測(cè)試用例之間共享數(shù)據(jù)。
- setup_data 函數(shù)會(huì)在 test_upper 和 test_fixture_teardown 方法之前執(zhí)行。
3. Pytest-cov
pytest-cov 是一個(gè)用于生成代碼覆蓋率報(bào)告的插件,它可以與 pytest 配合使用。
安裝:
pip install pytest-cov
基本用法:
def add(a, b):
return a + b
def test_add():
assert add(1, 2) == 3
def test_add_negative():
assert add(-1, -1) == -2
定義一個(gè)簡(jiǎn)單的 add 函數(shù)和兩個(gè)測(cè)試用例。
運(yùn)行測(cè)試并生成覆蓋率報(bào)告:
pytest --cov=my_module
--cov=my_module 參數(shù)指定要生成覆蓋率報(bào)告的模塊。
輸出示例:
============================= test session starts ==============================
platform darwin -- Python 3.10.7, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /path/to/your/project
plugins: cov-3.0.0
collected 2 items
tests/test_my_module.py .. [100%]
----------- coverage: platform darwin, python 3.10.7-final-0 -----------
Name Stmts Miss Cover Missing
-------------------------------------------------
my_module.py 1 0 100%
-------------------------------------------------
TOTAL 1 0 100%
4. Nose2
nose2 是 nose 的改進(jìn)版,它支持更多的測(cè)試發(fā)現(xiàn)機(jī)制和插件。
安裝:
pip install nose2
基本用法:
import unittest
class TestAdd(unittest.TestCase):
def test_add_positive(self):
self.assertEqual(add(1, 2), 3)
def test_add_negative(self):
self.assertEqual(add(-1, -1), -2)
定義一個(gè)測(cè)試類 TestAdd。
運(yùn)行測(cè)試:
nose2
輸出示例:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
5. Hypothesis
hypothesis 是一個(gè)強(qiáng)大的參數(shù)化測(cè)試庫(kù),可以生成大量隨機(jī)數(shù)據(jù)進(jìn)行測(cè)試。
安裝:
pip install hypothesis
基本用法:
from hypothesis import given, strategies as st
from my_module import add
@given(st.integers(), st.integers())
def test_add(a, b):
assert add(a, b) == a + b
- 使用 @given 裝飾器定義測(cè)試函數(shù)。
- st.integers() 生成整數(shù)類型的隨機(jī)數(shù)據(jù)。
運(yùn)行測(cè)試:
pytest
輸出示例:
============================= test session starts ==============================
platform darwin -- Python 3.10.7, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /path/to/your/project
plugins: hypothesis-6.44.0
collected 1 item
tests/test_my_module.py . [100%]
============================== short test summary info ===============================
hypothesis passed 100 tests for test_add, 1.00% of examples were new[100%]
6. Doctest
doctest 是 Python 標(biāo)準(zhǔn)庫(kù)中的一個(gè)模塊,可以將文檔字符串中的示例作為測(cè)試用例。
基本用法:
def add(a, b):
"""
>>> add(1, 2)
3
>>> add(-1, -1)
-2
"""
return a + b
在文檔字符串中編寫(xiě)測(cè)試用例。
運(yùn)行測(cè)試:
python -m doctest my_module.py
輸出示例:
Trying:
add(1, 2)
Expecting:
3
ok
Trying:
add(-1, -1)
Expecting:
-2
ok
2 items had no tests:
my_module
my_module.add
1 items passed all tests:
2 tests in my_module.add
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
7. Pytest-Check
pytest-check 是 pytest 的一個(gè)插件,提供了一些方便的斷言函數(shù)。
安裝:
pip install pytest-check
基本用法:
from check import check
def test_add():
check.equal(add(1, 2), 3)
check.equal(add(-1, -1), -2)
使用 check.equal 斷言函數(shù)進(jìn)行驗(yàn)證。
運(yùn)行測(cè)試:
pytest
輸出示例:
============================= test session starts ==============================
platform darwin -- Python 3.10.7, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /path/to/your/project
plugins: check-0.2.0
collected 1 item
tests/test_my_module.py . [100%]
============================== short test summary info ===============================
1 passed in 0.01s
8. Pytest-Mock
pytest-mock 是一個(gè) pytest 插件,用于模擬對(duì)象的行為。
安裝:
pip install pytest-mock
基本用法:
from my_module import some_function
import pytest
def test_some_function(mocker):
mocker.patch('my_module.some_function', return_value=42)
result = some_function()
assert result == 42
使用 mocker.patch 模擬 some_function 的返回值。
運(yùn)行測(cè)試:
pytest
輸出示例:
============================= test session starts ==============================
platform darwin -- Python 3.10.7, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /path/to/your/project
plugins: mock-3.7.0
collected 1 item
tests/test_my_module.py . [100%]
============================== short test summary info ===============================
1 passed in 0.01s
實(shí)戰(zhàn)案例:在線購(gòu)物車系統(tǒng)
假設(shè)我們有一個(gè)在線購(gòu)物車系統(tǒng),用戶可以添加商品到購(gòu)物車,并查看總價(jià)。我們需要編寫(xiě)單元測(cè)試來(lái)確保系統(tǒng)的正確性。
代碼實(shí)現(xiàn):
# shopping_cart.py
class ShoppingCart:
def __init__(self):
self.items = []
def add_item(self, item_name, price, quantity=1):
self.items.append((item_name, price, quantity))
def get_total(self):
total = 0
for item in self.items:
total += item[1] * item[2]
return total
單元測(cè)試:
# test_shopping_cart.py
import pytest
from shopping_cart import ShoppingCart
def test_add_item():
cart = ShoppingCart()
cart.add_item("apple", 2.0, 2)
assert len(cart.items) == 1
def test_get_total():
cart = ShoppingCart()
cart.add_item("apple", 2.0, 2)
cart.add_item("banana", 1.5, 3)
assert cart.get_total() == 2 * 2.0 + 3 * 1.5
運(yùn)行測(cè)試:
pytest
輸出示例:
============================= test session starts ==============================
platform darwin -- Python 3.10.7, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /path/to/your/project
collected 2 items
test_shopping_cart.py .. [100%]
============================== short test summary info ===============================
2 passed in 0.01s
總結(jié)
本文介紹了 Python 中常用的幾種單元測(cè)試框架及其基本用法,包括 unittest、pytest、pytest-cov、nose2、hypothesis、doctest、pytest-check 和 pytest-mock。通過(guò)實(shí)戰(zhàn)案例展示了如何使用這些框架編寫(xiě)有效的單元測(cè)試,幫助確保代碼的質(zhì)量和穩(wěn)定性。