編寫(xiě)測(cè)試腳本以確保你的代碼正常工作,被認(rèn)為是一種良好的編程實(shí)踐。Python生態(tài)系統(tǒng)有許多測(cè)試框架,包括捆綁在標(biāo)準(zhǔn)庫(kù)中的 unittest 。 Pytest 是一個(gè)流行的測(cè)試庫(kù)。它是Pyramid項(xiàng)目的一個(gè)首選庫(kù)。
在演示 PasteDeploy 配置的使用時(shí),我們將使用我們先前開(kāi)發(fā)的hello包。
首先,確保Pyramid環(huán)境中安裝了 PyTest 包。
pip3 install pytest
打開(kāi)hello包中的 setup.py 文件,加入黑體字顯示的行,對(duì)其進(jìn)行修改。
from setuptools import setup
requires = [
'pyramid',
'waitress',
]
dev_requires = ['pytest',]
setup(
name='hello',
install_requires=requires,
extras_require={
'dev': dev_requires,
},
entry_points={
'paste.app_factory': [
'main = hello:main'
],
},
)
在這里,只要使用以下命令安裝(或重新安裝)Pytest,它就會(huì)被添加為項(xiàng)目的依賴關(guān)系—-。
pip3 install -e ".[dev]
將以下Python代碼作為testing.py存儲(chǔ)在hello包中。
import unittest
from pyramid import testing
class HelloTests(unittest.TestCase):
def test_hello_world(self):
from . import hello_world
request = testing.DummyRequest()
response = hello_world(request)
self.assertEqual(response.status_code, 200)
要運(yùn)行這些測(cè)試,請(qǐng)使用下面的Pytest命令。測(cè)試的輸出如下所示
Env\hello>pytest tests.py
========================== test session starts ==========================
platform win32 -- Python 3.10.1, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\tp-pyramid\hello
collected 1 item
tests.py.
[100%]
=========================== 1 passed in 1.12s ===========================
要檢查測(cè)試是否失敗,在測(cè)試函數(shù)中誘發(fā)一個(gè)錯(cuò)誤并再次運(yùn)行。
(tp-pyramid) E:\tp-pyramid\hello>pytest tests.py
========================== test session starts ==========================
collected 1 item
tests.py F
[100%]
=============================== FAILURES ================================
______________________ HelloTests.test_hello_world ______________________
self = <hello.tests.HelloTests testMethod=test_hello_world>
def test_hello_world(self):
from . import hello_world
request = testing.DummyRequest()
response = hello_world(request)
> self.assertEqual(response.status_code, 404)
E AssertionError: 200 != 404
tests.py:13: AssertionError
======================== short test summary info ========================
FAILED tests.py::HelloTests::test_hello_world - AssertionError: 200 != 404
=========================== 1 failed in 1.53s ===========================
盡管單元測(cè)試在測(cè)試驅(qū)動(dòng)開(kāi)發(fā)(TDD)方法中被廣泛使用,但對(duì)于Web應(yīng)用程序來(lái)說(shuō), WebTest 是一個(gè)可以進(jìn)行功能測(cè)試的Python包。我們可以模擬一個(gè)針對(duì)WSGI應(yīng)用程序的完整HTTP請(qǐng)求,然后測(cè)試響應(yīng)中的信息。
讓我們使用前面例子中使用的hello項(xiàng)目。打開(kāi)setup.py并添加WebTest作為項(xiàng)目依賴。
from setuptools import setup
requires = [
'pyramid',
'waitress',
]
dev_requires = ['pytest','webtest',]
setup(
name='hello',
install_requires=requires,
extras_require={
'dev': dev_requires,
},
entry_points={
'paste.app_factory': [
'main = hello:main'
],
},
)
重新安裝hello包及其開(kāi)發(fā)模式的新依賴。
Env\hello>..\scripts\pip3 install -e ".[dev]"
在 test.py 文件中包含一個(gè)功能測(cè)試
import unittest
from pyramid import testing
class HelloTests(unittest.TestCase):
def test_hello_world(self):
from . import hello_world
request = testing.DummyRequest()
response = hello_world(request)
self.assertEqual(response.status_code, 200)
class HelloFunctionalTests(unittest.TestCase):
def setUp(self):
from . import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)
def test_hello_world(self):
res = self.testapp.get('/', status=200)
self.assertIn(b'<h1>Hello World!</h1>', res.body)
最后按照以下命令運(yùn)行Pytest —
Env\hello>pytest tests.py
========================== test session starts ==========================
platform win32 -- Python 3.10.1, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\tp-pyramid\hello
collected 2 items
tests.py .. [100%]
=========================== 2 passed in 2.37s ===========================
CookieCutter工具自動(dòng)生成了包含功能測(cè)試和單元測(cè)試的測(cè)試包。我們之前用Cookiecutter建立了名為testproj的Pyramid項(xiàng)目。在這個(gè)項(xiàng)目中,我們找到了測(cè)試文件夾。
test_functional py包含以下測(cè)試函數(shù)
from testproj import models
def test_my_view_success(testapp, dbsession):
model = models.MyModel(name='one', value=55)
dbsession.add(model)
dbsession.flush()
res = testapp.get('/', status=200)
assert res.body
def test_notfound(testapp):
res = testapp.get('/badurl', status=404)
assert res.status_code == 404
test_views.py定義了以下測(cè)試函數(shù)來(lái)測(cè)試視圖 –
from testproj import models
from testproj.views.default import my_view
from testproj.views.notfound import notfound_view
def test_my_view_failure(app_request):
info = my_view(app_request)
assert info.status_int == 500
def test_my_view_success(app_request, dbsession):
model = models.MyModel(name='one', value=55)
dbsession.add(model)
dbsession.flush()
info = my_view(app_request)
assert app_request.response.status_int == 200
assert info['one'].name == 'one'
assert info['project'] == 'testproj'
def test_notfound_view(app_request):
info = notfound_view(app_request)
assert app_request.response.status_int == 404
assert info == {}
這些測(cè)試由以下命令運(yùn)行 –
Env\testproj>Pytest
========================== test session starts ==========================
platform win32 -- Python 3.10.1, pytest-7.1.2, pluggy-1.0.0
rootdir: Env\testproj, configfile: pytest.ini, testpaths: testproj, tests
plugins: cov-3.0.0
collected 5 items
tests\test_functional.py .. [ 40%]
tests\test_views.py ... [100%]
=============== 5 passed, 20 warnings in 6.66s ===============
更多建議: