The method of testing flashes with session['_flashes'] didn't work for me, because session object simply doesn't have '_flashes' attribute in my case:
with client.session_transaction() as session:
flash_message = dict(session['_flashes']).get('warning')
KeyError: '_flashes
'
It might be because most recent version of flask and other packages I use with Python 3.6.4 may work differently, I honestly don't know...
What worked for me is a simple and straightforward:
def test_flash(self):
# attempt login with wrong credentials
response = self.client.post('/authenticate/', data={
'email': '[email protected]',
'password': '1234'
}, follow_redirects=True)
self.assertTrue(re.search('Invalid username or password',
response.get_data(as_text=True)))
In my case the flash message was 'Invalid user name or password'.
I think it's also easier to read. Hope it helps those who encountered a similar issue