test_ok2/doc/unittest.txt

49 lines
1.5 KiB
Plaintext
Raw Normal View History

2010-11-06 06:37:25 +08:00
.. _`unittest.TestCase`:
2010-11-02 07:53:53 +08:00
unittest.TestCase support
=====================================================================
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
of treating tests such as IO capturing::
# 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 ============================
platform linux2 -- Python 2.6.6 -- pytest-2.0.3
2010-11-26 20:26:56 +08:00
collecting ... collected 1 items
test_unittest.py F
================================= FAILURES =================================
____________________________ MyTest.test_method ____________________________
self = <test_unittest.MyTest testMethod=test_method>
def test_method(self):
x = 1
> self.assertEquals(x, 3)
E AssertionError: 1 != 3
test_unittest.py:8: AssertionError
----------------------------- Captured stdout ------------------------------
hello
========================= 1 failed in 0.02 seconds =========================
.. _`unittest.py style`: http://docs.python.org/library/unittest.html