![](/img/trans.png)
[英]Encoding: TypeError: write() argument must be str, not bytes
[英]TypeError: write() argument must be str, not bytes, UTF-16
因此,我正在使用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
测试报告生成器的代码位于https://github.com/tungwaiyip/HTMLTestRunner/blob/master/HTMLTestRunner.py创建者的github页面。 我该如何解决?
PS:如果有帮助,我正在运行python 3.5版。
HTMLTestRunner
是python2模块。 Python3区分str
和bytes
对象,而python2具有unicode
和str
。
如错误消息所述,行688需要str
而不是bytes
对象。 正如文档所阐明的那样 , str.encode
将str
对象转换为bytes
对象。 代替self.stream.write(output.encode('UTF-16'))
,您需要将第688行修改为self.stream.write(output)
。
请注意,由于python2 / 3不兼容,很可能会出现更多错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.