简体   繁体   中英

TypeError: write() argument must be str, not bytes, UTF-16

So I'm running test cases with Selenium and python, and I want to generate HTML Test reports for these tests. I found this resource that should do it for me, http://tungwaiyip.info/software/HTMLTestRunner.html in case anybody is interested it seems really good, but I keep getting this error.

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

The code for the test report generator is here https://github.com/tungwaiyip/HTMLTestRunner/blob/master/HTMLTestRunner.py the creator's github page. How do I fix this?

ps I'm running python version 3.5 if that helps.

HTMLTestRunner is a python2 module. Python3 distinguishes between str and bytes object, whereas python2 had unicode and str .

As the error message says, line 688 requires a str , not bytes object. As the documentation clarifies , str.encode converts a str object to a bytes object. Instead of self.stream.write(output.encode('UTF-16')) , you need to modify line 688 to self.stream.write(output) .

Note that there will very likely be more errors due to python2/3 incompatibilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM