2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
.. _`unittest.TestCase`:
|
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
unittest.TestCase support
|
2010-10-12 16:59:04 +08:00
|
|
|
=====================================================================
|
|
|
|
|
|
|
|
py.test has limited support for running Python `unittest.py style`_ tests.
|
|
|
|
It will automatically collect ``unittest.TestCase`` subclasses
|
|
|
|
and their ``test`` methods in test files. It will invoke
|
|
|
|
``setUp/tearDown`` methods but also perform py.test's standard ways
|
2010-11-02 07:53:53 +08:00
|
|
|
of treating tests like e.g. IO capturing::
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
# content of test_unittest.py
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
class MyTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
print ("hello") # output is captured
|
|
|
|
def test_method(self):
|
|
|
|
x = 1
|
|
|
|
self.assertEquals(x, 3)
|
|
|
|
|
|
|
|
Running it yields::
|
|
|
|
|
|
|
|
$ py.test test_unittest.py
|
|
|
|
=========================== test session starts ============================
|
2010-11-18 05:12:16 +08:00
|
|
|
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev30
|
2010-10-12 16:59:04 +08:00
|
|
|
test path 1: test_unittest.py
|
|
|
|
|
|
|
|
test_unittest.py F
|
|
|
|
|
|
|
|
================================= FAILURES =================================
|
|
|
|
____________________________ MyTest.test_method ____________________________
|
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
self = <test_unittest.MyTest testMethod=test_method>
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
x = 1
|
|
|
|
> self.assertEquals(x, 3)
|
|
|
|
|
|
|
|
test_unittest.py:8:
|
|
|
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
self = <test_unittest.MyTest testMethod=test_method>, first = 1, second = 3
|
2010-10-12 16:59:04 +08:00
|
|
|
msg = None
|
|
|
|
|
|
|
|
def failUnlessEqual(self, first, second, msg=None):
|
|
|
|
"""Fail if the two objects are unequal as determined by the '=='
|
|
|
|
operator.
|
|
|
|
"""
|
|
|
|
if not first == second:
|
|
|
|
raise self.failureException, \
|
|
|
|
> (msg or '%r != %r' % (first, second))
|
|
|
|
E AssertionError: 1 != 3
|
|
|
|
|
|
|
|
/usr/lib/python2.6/unittest.py:350: AssertionError
|
|
|
|
----------------------------- Captured stdout ------------------------------
|
|
|
|
hello
|
2010-11-18 05:12:16 +08:00
|
|
|
========================= 1 failed in 0.02 seconds =========================
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
.. _`unittest.py style`: http://docs.python.org/library/unittest.html
|
|
|
|
|