Dive-Into-Python-3 を写経して
いる最中、unit testの結果をrspec --color のようにred/greenでカラフルにしたくなりました。
個人的にはrubyのrspec が好きですが、python3 では unittest が標準のツールとして用意されていて、即使えて便利です。
import unittest # unittest.TestCase を継承したclassを作り # testで始まるメソッドを作成すればok class KnownValues(unittest.TestCase): def test_to_roman_konw_values(self): '''to_roman should give known result with known input''' for integer, numeral in self.known_values: result = roman.to_roman(integer) self.assertEqual(numeral, result) if __name__ == '__main__': unittest.main()
$ python3 roman_test.py -v ◼ test_blank (__main__.FromRomanBadInput) from_roman should fail with blank string ... ok test_malformed_antecedents (__main__.FromRomanBadInput) from_roman should fail with malformed antecedents ... ok test_repeated_pairs (__main__.FromRomanBadInput) from_roman should fail with repeated pairs of numerals ... ok test_too_many_repeated_numerals (__main__.FromRomanBadInput) from_roman should fail with too many repeated numerals ... ok test_from_roman_known_values (__main__.KnownValues) from_roman should give known result with known input ... ok test_to_roman_konw_values (__main__.KnownValues) to_roman should give known result with known input ... ok test_roundtrip (__main__.RoundtripCheck) from_roman(to_roman(n))==n for all n ... ok test_negative (__main__.ToRomanBadInput) to_roman should fail with negative input ... ok test_non_integer (__main__.ToRomanBadInput) to_roman should fail with non-integer input ... ok test_too_large (__main__.ToRomanBadInput) to_roman should fail with large input ... ok test_zero (__main__.ToRomanBadInput) to_roman should fail with 0 input ... ok ---------------------------------------------------------------------- Ran 11 tests in 0.014s OK
これを見やすくしたい。
本題
python3とpython2で利用出来るツールが違うようですが、
python3 だと CleanCut/green が使えました。
カラフル度は、githubページを見ていただくと分かるかと思います。
helpには、verboseオプションは2段階のように記載されていますが、
試しに実施してみると3段階まであるようです。
いろいろ機能があるらしく、coverage取れたりとかもできるようで、なかなか良さそうです。
(まずは、すぐ忘れるのでメモ)