Reputation: 45
I am trying to create an HTML Report for my unit Test Script, When i try to run the code, it is throwing, this error AttributeError: 'str' object has no attribute 'decode'
Below is the part of code, where it is showing error :-
if isinstance(o,str):
# TODO: some problem with 'string_escape': it escape \n and mess up formating
# uo = unicode(o.encode('string_escape'))
uo = o.decode('latin-1')
else:
uo = o
if isinstance(e,str):
# TODO: some problem with 'string_escape': it escape \n and mess up formating
# ue = unicode(e.encode('string_escape'))
ue = e.decode('latin-1')
else:
ue = e
script = self.REPORT_TEST_OUTPUT_TMPL % dict(
id = tid,
output = saxutils.escape(uo+ue),
)
The above code is from HTMLTestRunner.py
File. Please help in debugging this issue.
Upvotes: 0
Views: 3001
Reputation: 2395
I assume you are using python3 (because of the tag in your question)
In python3 there is no longer a unicode
type, it is simply str
- str
is a text type, which is already unicode decoded, therefore, there is no longer a decode
method for str
.
For working with strings, there is the bytes
type that has a decode
method (decode
-ing bytes
return str
, and encode
-ing str
returns bytes
.
So from now on - instead of using decode
when the type is str
, use decode
only if the type is bytes
.
Meaning your code should look like this:
if isinstance(o,bytes):
uo = o.decode('latin-1')
else:
uo = o
if isinstance(e,bytes):
ue = e.decode('latin-1')
else:
ue = e
script = self.REPORT_TEST_OUTPUT_TMPL % dict(
id = tid,
output = saxutils.escape(uo+ue),
)
Upvotes: 3