我正在使用http://nedbatchelder.com/code/coverage/中的coverage.py获取我的django单元测试的覆盖率数据,但我似乎无法弄清楚如何在django调试服务器上运行覆盖。我想使用:
运行django服务器coverage -x manage.py runserver 0.0.0.0:8080
当停止此服务器时,我应该获取覆盖数据,我可以使用它生成覆盖率报告。但是在使用CTRL-c关闭服务器时,我得到了
^ CCoverage.py警告:未收集任何数据。
如果无法使用此coverage.py脚本,我准备使用任何其他工具。
更新:
使用--noreload它似乎确实有效但在生成html覆盖率报告时我得到以下堆栈跟踪:
traceback (most recent call last):
File "/usr/local/bin/coverage", line 8, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.6/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.6/dist-packages/coverage/cmdline.py", line 549, in command_line
directory=options.directory, **report_args)
File "/usr/local/lib/python2.6/dist-packages/coverage/control.py", line 599, in html_report
reporter.report(morfs, config=self.config)
File "/usr/local/lib/python2.6/dist-packages/coverage/html.py", line 83, in report
self.report_files(self.html_file, morfs, config, config.html_dir)
File "/usr/local/lib/python2.6/dist-packages/coverage/report.py", line 86, in report_files
report_fn(cu, self.coverage._analyze(cu))
File "/usr/local/lib/python2.6/dist-packages/coverage/html.py", line 198, in html_file
self.write_html(html_path, html)
File "/usr/local/lib/python2.6/dist-packages/coverage/html.py", line 103, in write_html
write_encoded(fname, html, 'ascii', 'xmlcharrefreplace')
File "/usr/local/lib/python2.6/dist-packages/coverage/backward.py", line 137, in write_encoded
f.write(text.decode('utf8'))
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdf in position 59130: invalid continuation byte
答案 0 :(得分:3)
$ django-admin.py startproject mysite
$ cd mysite
$ coverage run manage.py runserver
Validating models...
0 errors found
Django version 1.3, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
^C
$ coverage report
Name Stmts Miss Cover
------------------------------
__init__ 0 0 100%
manage 11 4 64%
settings 24 0 100%
------------------------------
TOTAL 35 4 89%
什么操作系统,Django和覆盖版本等?您项目中还有其他不寻常之处吗?
单独测量runserver
可能不是您想要的,您可能只测量监视文件更改的代码并重新启动服务器。具有覆盖支持的Django测试运行器可以更好地满足您的需求。
更新:
提供--noreload开关可以覆盖所有代码。