py.test は、Python の unittest スタイル のテストに制限付きで対応しています。テストファイル内の unittest.TestCase のサブクラスとその test メソッドを自動的に探します。 setUp/tearDown メソッドを実行しますが、IO キャプチャのようなテストの扱いは pytest の標準的な方法で行います:
# test_unittest.py の内容
import unittest
class MyTest(unittest.TestCase):
def setUp(self):
print ("hello") # 出力内容をキャプチャ
def test_method(self):
x = 1
self.assertEquals(x, 3)
このコードを実行すると次のようになります:
$ py.test test_unittest.py
=========================== test session starts ============================
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
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.01 seconds =========================