2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
.. _`unittest.TestCase`:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Support for unittest.TestCase
|
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
|
2011-03-04 06:40:38 +08:00
|
|
|
of treating tests such as 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 ============================
|
2012-07-16 16:47:41 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
|
|
|
|
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
|
2010-11-26 20:26:56 +08:00
|
|
|
collecting ... collected 1 items
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
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)
|
2011-03-09 17:58:36 +08:00
|
|
|
E AssertionError: 1 != 3
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2011-03-09 17:58:36 +08:00
|
|
|
test_unittest.py:8: AssertionError
|
2010-10-12 16:59:04 +08:00
|
|
|
----------------------------- Captured stdout ------------------------------
|
|
|
|
hello
|
2012-07-16 16:47:41 +08:00
|
|
|
========================= 1 failed in 0.03 seconds =========================
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
.. _`unittest.py style`: http://docs.python.org/library/unittest.html
|
|
|
|
|