jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
501 stars 110 forks source link

assert_template_used fails to raise AssertionError #103

Closed havok2063 closed 7 years ago

havok2063 commented 7 years ago

It seems there's a bug when using the assert_template_used. When an improper template is specified, it fails to throw the AssertionError. It looks like a TypeError is thrown when attempting to throw the AssertionError.

Example test with wrong template specified. The correct template is galaxy.html and it works as expected when that is specified. index.html is a real template, but incorrect one used inside this route. I've also tried simply a misspelled template name, and a blank template name, and all three cast this error.

    def test_assert_galaxy_template_used(self):
        url = self.get_url('Galaxy:index')
        self._load_page('get', url)
        self.assertEqual('', self.data)
        self.assert_template_used('index.html')
Traceback (most recent call last):
  File "test_galaxy.py", line 47, in test_assert_galaxy_template_used
    self.assert_template_used('index.html')
  File "/Users/Brian/anaconda2/lib/python2.7/site-packages/flask_testing/utils.py", line 224, in assertTemplateUsed
    raise AssertionError("template %s not used. Templates were used: %s" % (name, ' '.join(used_templates)))
TypeError: sequence item 0: expected string, Template found
jcomo commented 7 years ago

Thanks for raising this issue -- good catch. Would you be comfortable submitting a PR to fix this and including your test cases in our own suite?

havok2063 commented 7 years ago

Yeah I think I can do that.

jcomo commented 7 years ago

Great! I'm happy to provide support if needed

havok2063 commented 7 years ago

The errors could not be recreated using the source code, due to a difference between the source code, and the pip-installed code.

Source code 0.6.1: raise AssertionError("Template %s not used. Templates were used: %s" % (name, ' '.join(repr(used_templates))))

Pip-installed code 0.6.1: raise AssertionError("template %s not used. Templates were used: %s" % (name, ' '.join(used_templates)))

The source code has the Template object wrapped in repr to format it into the string proper, whereas the pip-code does not. I suggest a new pip release be made to reflect this and any other changes that have been made. No fix was implemented but new tests were written nevertheless and I just submitted a new PR.

jcomo commented 7 years ago

Duplicate of https://github.com/jarus/flask-testing/pull/96