test パッケージ用のテストを書く場合、unittest モジュールを使い、以下のいくつかのガイドラインに従うよう推奨します。 一つは、テストモジュールの名前と同様、すべてのテストメソッドの 名前を"test_"で始めることです。これはテスト駆動プログラムに そのメソッドをテストメソッドとして認識させるためです。 また、テストメソッドにはドキュメンテーション文字列を入れるべきでは ありません。 テストメソッドのドキュメント記述には、 ("# True あるいは False だけを返すテスト関数" のような) コメントを使ってください。 これは、ドキュメンテーション文字列が存在する場合にはその内容が出力 されるため、どのテストを実行しているのかをいちいち表示しなくするためです。
以下のような基本的な決まり文句を使います:
import unittest
from test import test_support
class MyTestCase1(unittest.TestCase):
# Only use setUp() and tearDown() if necessary
def setUp(self):
... code to execute in preparation for tests ...
def tearDown(self):
... code to execute to clean up after tests ...
def test_feature_one(self):
# Test feature one.
... testing code ...
def test_feature_two(self):
# Test feature two.
... testing code ...
... more test methods ...
class MyTestCase2(unittest.TestCase):
... same structure as MyTestCase1 ...
... more test classes ...
def test_main():
test_support.run_unittest(MyTestCase1,
MyTestCase2,
... list other tests ...
)
if __name__ == '__main__':
test_main()
この定型的なコードによって、テストスイートをregrtest.py から起動できると同時に、スクリプト自体からも実行できるようになります。
回帰テストの目的はコードの分解です。 そのためには以下のいくつかのガイドラインに従ってください:
class TestFuncAcceptsSequences(unittest.TestCase):
func = mySuperWhammyFunction
def test_func(self):
self.func(self.arg)
class AcceptLists(TestFuncAcceptsSequences):
arg = [1,2,3]
class AcceptStrings(TestFuncAcceptsSequences):
arg = 'abc'
class AcceptTuples(TestFuncAcceptsSequences):
arg = (1,2,3)
参考:
ご意見やご指摘をお寄せになりたい方は、 このドキュメントについて... をご覧ください。