python - How to assert variable content in unit test (Flask, Python3, Nose2) -
i have flask app page content comes global variable. i'm trying set unit testing assert data, can't seem local variable work:
test_string = foo self.assertin(b['test_string'], response.data)
fails with:
nameerror: name 'b' not defined
if reference plain variable:
self.assertin(test_string, response.data)
i expected failure:
typeerror: bytes-like object required, not 'str'
the test succeeds if hard-code variable data test, i'd rather not have update test if variable changes. missing here?
the problem bytes literal prefix b
:
bytes literals prefixed 'b' or 'b'; produce instance of bytes type instead of str type. may contain ascii characters; bytes numeric value of 128 or greater must expressed escapes.
while sounds bytesprefix = bytes type, not seem work if data comes variable.
the solution change b
prefix bytes functionbytes
, states:
bytes objects can created literals, see string , bytes literals.
so while appear interchangeable, not case!
for use case, had specify encoding type bytes
function.
here working syntax original post's example:
self.assertin(bytes(test_string, "utf-8"), response.data)
thanks goes john gordon suggested switching bytes
in comments, never officially answered. after few days, i'll go ahead , close out now.
Comments
Post a Comment