11
11

More than 5 years have passed since last update.

[Python2.7] unittest の使い方まとめ

Last updated at Posted at 2016-11-08

マニュアル

25.3. unittest — ユニットテストフレームワーク — Python 2.7.x ドキュメント

テンプレート

すでに雛形を作成されている方がいたため、以下などを参照すると良いと思います。
python unittestのテンプレート - Qiita

最小構成のテストの例としては以下になります。

最小構成のテスト例
import unittest

class UnitTest(unittest.TestCase):
    def test1(self):
        self.assertEqual(1, 1)

if __name__ == '__main__':
    unittest.main()
実行例
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

※注意点
実行対象のテストは、メソッド名の先頭が test で始まる必要があります。

assertメソッド

Method 評価内容
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

詳細は以下を参照願います。その他にもいろいろあります。
25.3. unittest — ユニットテストフレームワーク — Python 2.7.x ドキュメント -

初期化時/終了時に特定処理を実行

Test全体初期化時/終了時

Test全体初期化時に実行
def setUpModule():
    pass
Test全体終了時に実行
def tearDownModule():
    pass

TestCase初期化時/終了時

Testcase初期化時に実行
class HogeTest(unittest.TestCase):
#...
    @classmethod
    def setUpClass(cls):
        pass
TestCase終了時に実行
class HogeTest(unittest.TestCase):
#...
    @classmethod
    def tearDownClass(cls):
        pass

TestFixture(TastCase内各テスト)初期化時/終了時

Testcase初期化時に実行
class HogeTest(unittest.TestCase):
#...
    def setUp(self):
        pass
TestCase終了時に実行
class HogeTest(unittest.TestCase):
#...
    def tearDown(self):
        pass

実行順

以下のようなテストを実行した場合のログを示します。

各関数の実行順確認サンプル(抜粋)
def setUpModule():
    print '## setUpModule!'

def tearDownModule():
    print '## tearDownModule!'
    print unittest.TestResult

class HogeTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print '## setUpClass!'
        pass

    @classmethod
    def tearDownClass(cls):
        print '## tearDownClass!'

    def setUp(self):
        print '## setUp!'

    def tearDown(self):
        print '## tearDown!'

    def test1(self):
        expected = 1
        actual = 1
        self.assertEqual(expected, actual)
実行結果
.....
----------------------------------------------------------------------
Ran 1 tests in 0.000s

OK
## setUpModule!
## setUpClass!
## setUp!
## tearDown!
## tearDownClass!
## tearDownModule!

テスト結果を取得する

TestResult class で取得できます。使用頻度の高い unittest.main() では以下のように実装します。

unittest.main()で取得する場合
def main()
    # デフォルトでは sys.exit() が呼ばれてしまうため、exit=Falseを指定する
    test_program = unittest.main(exit=False)
    test_result = test_program.result

TestResult で取得できる内容は以下を参照して下さい。

25.3. unittest — ユニットテストフレームワーク — Python 2.7.x ドキュメント

各テストの実行時間を変数として取得する

python - How to know time spent on each test when using unittest? - Stack Overflow

参考

python unittestのテンプレート - Qiita
ライブラリ:unittest - Life with Python
python - Unittest causing sys.exit() - Stack Overflow
pythonのunittestでテストを書く時、失敗する可能性のあるsetUpにはdoCleanupsを使う - Qiita

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11