类型错误:write() 参数必须是 str,而不是 bytes,UTF-16

use*_*577 2 python selenium typeerror selenium-webdriver

所以我正在使用 Selenium 和 python 运行测试用例,并且我想为这些测试生成 HTML 测试报告。我发现这个资源应该为我做这件事,http://tungwaiyip.info/software/HTMLTestRunner.html,以防有人感兴趣,它看起来真的很好,但我不断收到此错误。

File "facebook.py", line 21, in <module>
HTMLTestRunner.main()
File "C:\Users\kporika\AppData\Local\Programs\Python\Python35-32\lib\unittest\main.py", line 94, in __init__
self.runTests()
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 816, in runTests
unittest.TestProgram.runTests(self)
File "C:\Users\kporika\AppData\Local\Programs\Python\Python35-32\lib\unittest\main.py", line 255, in runTests
self.result = testRunner.run(self.test)
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 631, in run
self.generateReport(test, result)
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 688, in generateReport
self.stream.write(output.encode('UTF-16'))
TypeError: write() argument must be str, not bytes
Run Code Online (Sandbox Code Playgroud)

测试报告生成器的代码位于https://github.com/tungwaiyip/HTMLTestRunner/blob/master/HTMLTestRunner.py创建者的 github 页面。我该如何解决?

ps 我正在运行 python 3.5 版本,如果有帮助的话。

Mis*_*agi 5

HTMLTestRunner是一个python2模块。Python3 区分strbytes对象,而 python2 区分unicodestr

正如错误消息所示,第 688 行需要一个str, notbytes对象。正如文档所阐明的str.encodestr对象转换为bytes对象。相反self.stream.write(output.encode('UTF-16')),您需要将第 688 行修改为self.stream.write(output)

请注意,由于 python2/3 不兼容,很可能会出现更多错误。