Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python Selenium测试结果写入文件_Python_Selenium - Fatal编程技术网

将Python Selenium测试结果写入文件

将Python Selenium测试结果写入文件,python,selenium,Python,Selenium,我正在为使用SeleniumWebDriver用Python编写的一套测试用例生成报告。我正在用一个主脚本启动测试用例,我想把结果写入一个.txt文件,最终会通过电子邮件发送出去 当一个测试成功运行时,我在终端中得到如下结果: ---------------------------------------------------------------------- Ran 1 test in 15.566s OK 这是我想写入文件的内容。我试过了 python test.py > f

我正在为使用SeleniumWebDriver用Python编写的一套测试用例生成报告。我正在用一个主脚本启动测试用例,我想把结果写入一个.txt文件,最终会通过电子邮件发送出去

当一个测试成功运行时,我在终端中得到如下结果:

----------------------------------------------------------------------
Ran 1 test in 15.566s

OK
这是我想写入文件的内容。我试过了

python test.py > file
无济于事。我也试过了

 sys.stdout = open('Results.txt', 'w') 
这不管用。我只想写入一个测试通过或失败的文件,但它不会打印该部分。我知道这是Selenium代码的内部代码,但我不知道它在哪里/是什么。测试中的任何其他打印都将写入文件,但不会写入结果


谢谢你的建议

测试结果输出写入
stderr
。试一试

python test.py 2> file

非常感谢。成功了。现在我要做的就是打印测试的名称。如果我在脚本中加入一个普通的“print”语句,它将只显示在终端中,而不是文件中。有什么想法吗?@user3038369你很好!如果你找到了满足你需要的答案,你可以考虑。对于有关打印的问题,您可以使用
>文件2>&1
一次性捕获stdout和stderr,或者使用
导入系统打印到stderr;打印>>sys.stderr,“一些文本”
some。回答接受!你不知道我想弄明白这些事情有多久了!