2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
.. _`unittest.TestCase`:
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Support for unittest.TestCase / Integration of fixtures
|
2010-10-12 16:59:04 +08:00
|
|
|
=====================================================================
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
py.test has support for running Python `unittest.py style`_ tests.
|
2010-10-12 16:59:04 +08:00
|
|
|
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-10-09 20:35:17 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev20
|
2012-10-07 19:06:17 +08:00
|
|
|
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-10-07 19:06:17 +08:00
|
|
|
========================= 1 failed in 0.01 seconds =========================
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
.. _`unittest.py style`: http://docs.python.org/library/unittest.html
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Moreover, you can use pytest's new :ref:`autoactive fixtures`
|
|
|
|
functions, thereby connecting pytest's :ref:`fixture mechanism <fixture>`
|
|
|
|
with a setup/teardown style::
|
2012-09-18 16:54:12 +08:00
|
|
|
|
|
|
|
# content of test_unittest_funcargs.py
|
|
|
|
import pytest
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
class MyTest(unittest.TestCase):
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.fixture(autoactive=True)
|
2012-09-18 16:54:12 +08:00
|
|
|
def chdir(self, tmpdir):
|
|
|
|
tmpdir.chdir() # change to pytest-provided temporary directory
|
|
|
|
tmpdir.join("samplefile.ini").write("# testdata")
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
s = open("samplefile.ini").read()
|
|
|
|
assert "testdata" in s
|
|
|
|
|
|
|
|
Running this file should give us one passed test because the setup
|
|
|
|
function took care to prepare a directory with some test data
|
|
|
|
which the unittest-testcase method can now use::
|
|
|
|
|
|
|
|
$ py.test -q test_unittest_funcargs.py
|
|
|
|
.
|
|
|
|
|
|
|
|
If you want to make a database attribute available on unittest.TestCases
|
2012-10-07 19:06:17 +08:00
|
|
|
instances, you can do it using :ref:`usefixtures` and a simple
|
|
|
|
:ref:`fixture function`::
|
2012-09-18 16:54:12 +08:00
|
|
|
|
|
|
|
# content of test_unittest_marked_db.py
|
|
|
|
import pytest
|
|
|
|
import unittest
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def db(request):
|
2012-09-18 16:54:12 +08:00
|
|
|
class DummyDB:
|
2012-10-07 19:06:17 +08:00
|
|
|
entries = []
|
|
|
|
db = DummyDB()
|
|
|
|
if request.instance is not None:
|
2012-09-18 16:54:12 +08:00
|
|
|
request.instance.db = db
|
2012-10-07 19:06:17 +08:00
|
|
|
return db
|
2012-09-18 16:54:12 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.mark.usefixtures("db")
|
2012-09-18 16:54:12 +08:00
|
|
|
class MyTest(unittest.TestCase):
|
2012-10-07 19:06:17 +08:00
|
|
|
def test_append(self):
|
|
|
|
self.db.entries.append(1)
|
2012-09-18 16:54:12 +08:00
|
|
|
|
|
|
|
def test_method2(self):
|
2012-10-07 19:06:17 +08:00
|
|
|
# check we have a fresh instance
|
|
|
|
assert len(self.db.entries) == 0
|
2012-09-18 16:54:12 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Running it passes both tests::
|
2012-09-18 16:54:12 +08:00
|
|
|
|
|
|
|
$ py.test -q test_unittest_marked_db.py
|
|
|
|
..
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
If you rather want to provide a class-cached "db" attribute, you
|
|
|
|
can write a slightly different fixture using a ``scope`` parameter
|
|
|
|
for the fixture decorator ::
|
|
|
|
|
|
|
|
# content of test_unittest_class_db.py
|
|
|
|
import pytest
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def db_class(request):
|
|
|
|
class DummyDB:
|
|
|
|
entries = []
|
|
|
|
db = DummyDB()
|
|
|
|
if request.cls is not None:
|
|
|
|
request.cls.db = db
|
|
|
|
return db
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("db_class")
|
|
|
|
class MyTest(unittest.TestCase):
|
|
|
|
def test_append(self):
|
|
|
|
self.db.entries.append(1)
|
|
|
|
|
|
|
|
def test_method2(self):
|
|
|
|
# check we DONT have a fresh instance
|
|
|
|
assert len(self.db.entries) == 1
|
|
|
|
|
|
|
|
Running it again passes both tests::
|
|
|
|
|
|
|
|
$ py.test -q test_unittest_class_db.py
|
|
|
|
..
|