python - सही तरीके से यह कहने के लिए कि एक अपवाद pytest में उठाया जाता है?
unit-testing testing (4)
कोड:
# coding=utf-8
import pytest
def whatever():
return 9/0
def test_whatever():
try:
whatever()
except ZeroDivisionError as exc:
pytest.fail(exc, pytrace=True)
आउटपुट:
================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django, cov
collected 1 items
pytest_test.py F
====================================== FAILURES ======================================
___________________________________ test_whatever ____________________________________
def test_whatever():
try:
whatever()
except ZeroDivisionError as exc:
> pytest.fail(exc, pytrace=True)
E Failed: integer division or modulo by zero
pytest_test.py:12: Failed
============================== 1 failed in 1.16 seconds ==============================
सबसे ज्यादा प्रिंट ट्रेसबैक कैसे बनाएं, इसलिए मैं देखूंगा कि किसी whatever
कार्य को अपवाद उठाया गया था?
इस तरह के मामलों को पाइस्टेस्ट में संभालने के दो तरीके हैं:
pytest.raises
समारोह का उपयोग करनाpytest.mark.xfail
सजावट का उपयोग करना
pytest.raises
का उपयोग:
def whatever():
return 9/0
def test_whatever():
with pytest.raises(ZeroDivisionError):
whatever()
pytest.mark.xfail
का उपयोग:
@pytest.mark.xfail(raises=ZeroDivisionError)
def test_whatever():
whatever()
pytest.raises
आउटपुट:
============================= test session starts ============================
platform linux2 -- Python 2.7.10, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 --
/usr/local/python_2.7_10/bin/python
cachedir: .cache
rootdir: /home/ukpdl, inifile:
collected 1 item
test_fun.py::test_whatever PASSED
======================== 1 passed in 0.01 seconds =============================
pytest.xfail
मार्कर का आउटपुट:
============================= test session starts ============================
platform linux2 -- Python 2.7.10, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 --
/usr/local/python_2.7_10/bin/python
cachedir: .cache
rootdir: /home/ukpdl, inifile:
collected 1 item
test_fun.py::test_whatever xfail
======================== 1 xfailed in 0.03 seconds=============================
जैसा कि दस्तावेज़ कहता है:
pytest.raises
का उपयोग उन मामलों के लिए बेहतर होने की संभावना है जहां आप अपवादों का परीक्षण कर रहे हैं, आपका स्वयं का कोड जानबूझ कर उठा रहा है, जबकि चेक फ़ंक्शन के साथ @pytest.mark.xfail
का उपयोग करना शायद @pytest.mark.xfail
बग दस्तावेज़ (जैसे परीक्षण वर्णन करता है) "होना चाहिए" या निर्भरताओं में कीड़े।
क्या आपका मतलब कुछ इस तरह का था:
def test_raises():
with pytest.raises(Exception) as excinfo:
raise Exception('some info')
assert excinfo.value.message == 'some info'
तुम कोशिश कर सकते हो
def test_exception():
with pytest.raises(Exception) as excinfo:
function_that_raises_exception()
assert str(excinfo.value) == 'some info'
बेहतर अभ्यास एक वर्ग का उपयोग करेगा जो unittest.TestCase और self.assertRaises चला रहा है।
उदाहरण के लिए:
import unittest
def whatever():
return 9/0
class TestWhatEver(unittest.TestCase):
def test_whatever():
with self.assertRaises(ZeroDivisionError):
whatever()
फिर आप इसे चलाकर निष्पादित करेंगे:
pytest -vs test_path