py.testでユニットテスト
環境
$ python -VPython 3.3.0
pip-3.3がインストールされている
py.testのインストール
pip install pytest
すごいシンプルなユニットテスト - 1
test_simple.py
import unittest
class TestSimple(unittest.TestCase):
def test_equal(self):
self.assertEqual(3, 3)
def test_not_equal(self):
self.assertNotEqual(3, 4)
def test_ture(self):
self.assertTrue(6 == 6)
if __name__ == '__main__':
unittest.main()
実行結果
$ py.test simpleTest.py ========================== test session starts ========================== platform linux -- Python 3.3.0 -- pytest-2.3.4 collected 3 items test_simple.py ... ======================== 3 passed in 0.24 seconds ========================
すごいシンプルなユニットテスト - 2
maxNum.py
def maxNum(x, y):
if(x > y):
return x
else:
return y
if __name__ == '__name__':
maxNum(2, 3)
testMaxNum.py
import maxNum
import unittest
class TestNumMax(unittest.TestCase):
def test_numMax(self):
#maxNum.maxNum(2, 3)の結果が3かどうかテストする。3ならテスト成功
self.assertEqual(maxNum.maxNum(2, 3), 3)
if __name__ == '__main__':
unittest.main()
実行結果
$ py.test testMaxNum.py ========================== test session starts ========================== platform linux -- Python 3.3.0 -- pytest-2.3.4 collected 1 items test_MaxNum.py . ======================== 1 passed in 0.32 seconds ========================
注意
ファイル名は、test_*.py または *_test.py に するテストクラス名は、Test という接頭辞に する (__init__ メソッドをもたない)
テスト関数名やメソッド名は、test_ という接頭辞に する
Python 3.3 移行するならテスト環境を py.test と tox にすると便利
http://www.sakito.com/2012/09/python-33-pytest-tox.html
4データ駆動テストを nose と pytest でやってみた
http://d.hatena.ne.jp/t2y-1979/20120209/132874027
0 件のコメント:
コメントを投稿