Python Nose:使用多进程插件将测试结果记录到文件中

2024-04-29 19:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将我的测试输出记录到一个文件中,同时运行它们。 对于这个Im,尝试使用多进程插件和xunit插件。

我知道它们不在一起工作,xunit不会记录任何东西,因为multiprocess不会直接发送输出。

https://github.com/nose-devs/nose/issues/2

我要找的是任何一个可以让我把输出写进文件的选项。 原因是我正在运行Selenium测试,每次我得到一个错误,stacktrace都非常大,以至于stdout基本上是完全填满的。 一些缓解问题的方法也可能有帮助,selenium文档中关于如何配置日志输出的内容非常少。

我还尝试了stdout的基本重定向:

#nosetests > file.txt

但这也不管用。


Tags: 文件httpsgithubcom插件进程选项stdout
1条回答
网友
1楼 · 发布于 2024-04-29 19:28:11

如果要使用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

相关问题 更多 >