Tag Archives: Automatic test report generation by htmltestrunner

When generating a test report in HTML format, report [typeerror: a bytes like object is required, not ‘STR’]

1、 Error information

E:\pythonProject\study\venv\Scripts\python.exe E:/pythonProject/case/test_ login.py
…Traceback (most recent call last):
  File “E:\pythonProject\case\test_ login.py”, line 71, in < module>
    runner.run(suit)  # Call the run() method under the htmltestrunner class to run the use case suite
class   File “E:\pythonProject\study\venv\lib\site-packages\HTMLTestRunner\HTMLTestRunner.py”, line 631, in run
    self.generateReport(test, result)
  File “E:\pythonProject\study\venv\lib\site-packages\HTMLTestRunner\HTMLTestRunner.py”, line 691, in generateReport
    self.stream.write(output)
TypeError: a bytes-like object is required, not ‘str’

2、 Source code

#Path and name of test report

dir = “E:/pythonProject/login_ report.html”

#”WB” creates or opens a binary file and writes the finished data

filename = open(dir, “wb”)

#Call htmltestrunner class to define test report content

runner = HTMLTestRunner.HTMLTestRunner(stream=filename, title=”Testcase Report”, description=”testcases”)

runner.run(suit)     #  Call the run () method under the htmltestrunner class to run the use case suite

filename.close()    #  Close test report file

3、 Solutions

#Path and name of test report

dir = “E:/pythonProject/login_ report.html”

#”W” creates or opens a new file and writes the finished data

filename = open(dir, “w”)

#Call htmltestrunner class to define test report content

runner = HTMLTestRunner.HTMLTestRunner(stream=filename, title=”Testcase Report”, description=”testcases”)

runner.run(suit)     #  Call the run () method under the htmltestrunner class to run the use case suite

filename.close()    #  Close test report file