【问题标题】:Python Nose: Log tests results to a file with Multiprocess PluginPython Nose:使用多进程插件将测试结果记录到文件中
【发布时间】:2012-02-28 01:15:50
【问题描述】:
我正在尝试将我的测试输出记录到文件中并同时运行它们。
为此,我尝试使用多进程插件和 xunit 插件。
我知道它们不能一起工作,xunit 不记录任何内容,因为 mutiprocess 不直接发送输出。
https://github.com/nose-devs/nose/issues/2
我正在寻找的是允许我将输出写到文件中的任何替代方法。
原因是我正在运行 Selenium 测试,每次出现错误时,stacktrace 都很大,以至于 stdout 基本上被完全填满了。
一些缓解措施也可能有所帮助,关于如何配置日志输出的 selenium 文档非常缺乏。
我还尝试了一个非常基本的标准输出重定向:
#nosetests > file.txt
但这也不起作用。
【问题讨论】:
标签:
python
logging
multiprocessing
nose
【解决方案1】:
如果你想从 shell 使用基本重定向,你可以这样做
nosetests &> output.txt
但根据您的问题,您似乎更愿意做类似的事情:
$nosetests --processes 4 --with-xunit --xunit-file=test_output.xml
完整示例:
$ls
test_nose.py test_nose.pyc
$cat test_nose.py
import sys
import os
import time
def setUp():
pass
def test_1():
time.sleep(5)
with open('test_pid_' + str(os.getpid()), 'w') as f:
f.write(str(os.getpid()) + '\n')
def test_2():
time.sleep(5)
with open('test_pid_' + str(os.getpid()), 'w') as f:
f.write(str(os.getpid()) + '\n')
def test_3():
time.sleep(5)
with open('test_pid_' + str(os.getpid()), 'w') as f:
f.write(str(os.getpid()) + '\n')
def test_4():
time.sleep(5)
with open('test_pid_' + str(os.getpid()), 'w') as f:
f.write(str(os.getpid()) + '\n')
def tearDown():
pass
$ nosetests --processes 4 --with-xunit --xunit-file=test_output.xml
....
----------------------------------------------------------------------
Ran 4 tests in 5.223s
OK
$ ls
test_nose.py test_output.xml test_pid_55247 test_pid_55249
test_nose.pyc test_pid_55246 test_pid_55248
$ cat test_pid*
55246
55247
55248
55249
$ xmllint -format test_output.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="nosetests" tests="0" errors="0" failures="0" skip="0"/>
看起来不像你说的那样工作:)
但是
$nosetests --processes 4 &> output.txt
和
$nosetests --with-xunit --xunit-file=test_output.xml
做。
参考资料:
Redirect stderr and stdout in a Bash script
$man xmllint
$nosetests -h