Python: コード網羅率の可視化ツール
Python における Code coverage ツールは現時点において以下の2種類である。
インストールコマンドと共に記す。
・Code coverage (coverage.py) => easy_install coverage
・trace2html (trace2html.py) => easy_install trace2html
後者の方がより明解なHTMLで表示されるので、こちらを採用する。
・ヘルプ
1: Usage: trace2html.py [options] 2: 3: Utility to generate HTML test coverage reports for python programs 4: 5: Example usage 6: ------------- 7: 8: Use trace2html to directly compute the coverage of a test suite by 9: specifying the module you are interested in:: 10: 11: $ trace2html.py -w my_module --run-command ./my_testrunner.py 12: $ firefox coverage_dir/index.html 13: 14: Or you can collect coverage data generated with trace.py:: 15: 16: $ /usr/lib/python2.4/trace.py -mc -C coverage_dir -f counts my_testrunner.py 17: 18: Write a report in directory 'other_dir' from data collected in 'counts':: 19: 20: $ trace2html.py -f counts -o other_dir 21: $ firefox other_dir/index.html 22: 23: 24: 25: Options: 26: -h, --help show this help message and exit 27: -f COVERAGE_FILES, --coverage-file=COVERAGE_FILES 28: Use the content of a trace file 29: -o REPORT_DIR, --output-dir=REPORT_DIR 30: Directory to store the generated HTML report. Defaults 31: to 'coverage_dir' 32: -s CSS, --with-css-stylesheet=CSS 33: Use an alternative CSS stylesheet 34: -t, --self-test Run the tests for trace2html 35: -v, --verbose Set verbose mode on (cumulative) 36: -r, --run-command Collect coverage data by running the given python 37: script with all trailing arguments 38: -b BLACKLIST_MODS, --blacklist-module=BLACKLIST_MODS 39: Add a module to the black list 40: -B BLACKLIST_DIRS, --blacklist-dir=BLACKLIST_DIRS 41: Add a directory to the black list 42: -w WHITELIST_MODS, --whitelist-module=WHITELIST_MODS 43: Add a module to the white list 44: -W WHITELIST_DIRS, --whitelist-dir=WHITELIST_DIRS 45: Add a directory to the white list基本的な構文は、
trace2html.py –w Coverageを計測したいモジュール名 [-w モジュール名] –r ユニットテストモジュールのパス
といった塩梅。
・実行例
1: > trace2html.py -w shogi.state -w shogi.piece -r .\test_state.py
2: Testing... 3: ..... 4: ---------------------------------------------------------------------- 5: Ran 5 tests in 5.373s 6: 7: OK 8: report written to: カレントディレクトリ\coverage_dir\index.htmlすると、カレントディレクトリに「coverage_dir」というディレクトリが作成され、配下に計測結果のHTMLが格納される。
画面を遷移すると、ソースコードの各行ごとに処理された回数が表示され、未処理の行は赤く表示される。
参考:
http://pypi.python.org/pypi/trace2html
http://lab.hde.co.jp/2008/07/pythonunittestcodecoverage-1.html
0 件のコメント:
コメントを投稿