简体   繁体   中英

Unit test comparing response data raises TypeError

I'm trying to write some unit tests for the example flaskr application. I want to check if a specific string is in the response. I keep getting a TypeError: 'str' does not support the buffer interface . Why am I getting this error and how should I fix it?

======================================================================
ERROR: test_empty_db (__main__.FlaskrTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_flaskr.py", line 31, in test_empty_db
    assert 'No entries here so far' in rv.data
TypeError: 'str' does not support the buffer interface

Response data from the test client is bytes, not unicode, data. In Python 3, this means you need to decode the data to compare it to a string, or compare it to a bytestring instead.

# compare bytes
assert b'No entries so far' in rv.data

# or decode to string
assert 'No entries so far' in rv.data.decode('utf8')

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