Python 覆盖不会将输出重定向到文件

Python coverage does not redirect output to a file

我正在尝试 运行 覆盖 Python 单元测试文件 WorkTest.py,并将输出转发到文件 output.txt.

$ coverage run --branch WorkTest.py > output.txt

但是,输出一直出现在屏幕上,而我 运行 这个命令所在的文件夹中没有 output.txt。为什么覆盖无法将输出重定向到文件?

我有 Windows 10、python 3.6.8、覆盖率 5.1,以及 Git 附带的命令行 Git Bash。 谢谢。

首先检查输出是否实际上不在 stderr 而不是 stdout:

coverage run --branch WorkTest.py > output.txt 2>&1

Coverage.py command line usage确实提到:

The debug output goes to stderr, unless the COVERAGE_DEBUG_FILE environment variable names a different file, which will be appended to.