テストかくとき skip とdocstring メモ
ほえー
知らなかったのでまとめておく
テストを skip したいときは @skip デコレータが使える
参考::26.3. unittest — Unit testing framework — Python v3.4.0a1 documentation
実際は Django でやってるけど中身は似てるはず*1。まるまる引用すると
class MyTestCase(unittest.TestCase): @unittest.skip("demonstrating skipping") def test_nothing(self): self.fail("shouldn't happen") @unittest.skipIf(mylib.__version__ < (1, 3), "not supported in this library version") def test_format(self): # Tests that work for only a certain version of the library. pass @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows") def test_windows_support(self): # windows specific testing code pass
結果は
test_format (__main__.MyTestCase) ... skipped 'not supported in this library version' test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping' test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows' ---------------------------------------------------------------------- Ran 3 tests in 0.005s OK (skipped=3)
コメントアウトして TODO かいて pass してたけど、このほうがわかりやすいですね。
@kk6 ++
テストで落ちた時のために docstring は1行目にサマリーをかく
class MyTestCase(unittest.TestCase): """Myについてのテスト""" def test_nothing(self): """正常系""" self.assertEqual(1,1) def test_nothing2(self): """正常系2 複数行に渡る場合 """ self.assertEqual(1,1) def test_nothing3(self): """ 正常系3 複数行に渡る場合 """ self.assertEqual(1,1)
1行で簡単に収まるときはいいけど、そうじゃない時も test_nothing2 みたいに1行目にかくことで落ちた時にエラーメッセージとして表示してくれるらしい。これからはそうしよう。
ただ、テストコードじゃないコード部分で複数行の docstring は改行して test_nothing3 みたいに改行するほうが好きかな
*1:from django.utils.unittest import skip があった