Hi Andrew,
On Sun, 6 Apr 2014, Andrew Pashkin wrote:
* It makes it much harder to write custom assertions and get meaningful
display on error.
Can you give an examples for cases with messages/breakings and for custom
assertions?
I don't have an example of breakage to hand, I ripped out the offending
code in disgust and replaced it with standard assertions that worked. If I
find another example I'll be sure to let you know before I do the same
again.
Regarding custom assertions, how would you write assert_not_redirected
from
https://github.com/aptivate/django-harness/blob/master/django_harness/fast_dispatch.py
as a PyTest assertion? Consider the following tests:
def not_redirected(response):
return (response.status_code != 302)
class RedirectViewTests(FastDispatchMixin, TestCase):
def test_redirect_view_with_custom_assertion(self):
response = self.fast_dispatch('my-redirecting-view')
self.assert_not_redirected(response)
def test_redirect_view_with_pytest_assertion(self):
response = self.fast_dispatch('my-redirecting-view')
assert not_redirected(response)
assert_not_redirected is from FastDispatchMixin in
https://github.com/aptivate/django-harness/blob/master/django_harness/fast_dispatch.py,
not_redirected is a simple assertion I wrote as a demo, since I don't use
pytest much myself for this reason. Now compare the output (reformatted
for readability):
____________________________________
RedirectViewTests.test_redirect_view_with_custom_assertion
____________________________________
main/tests/tests.py:71: in test_redirect_view_with_custom_assertion
self.assert_not_redirected(response)
.ve/src/django-harness/django_harness/fast_dispatch.py:114: in
assert_not_redirected
msg_prefix)
E AssertionError: 302 != 200 : unexpectedly redirected to
http://www.google.com/
____________________________________
RedirectViewTests.test_redirect_view_with_pytest_assertion
____________________________________
main/tests/tests.py:75: in test_redirect_view_with_pytest_assertion
assert not_redirected(response)
E AssertionError: assert
not_redirected(<django.http.response.HttpResponseRedirect object at
0x493da10>)
I.e. my assertion outputs:
AssertionError: 302 != 200 : unexpectedly redirected to
http://www.google.com/
and the simple Python assertion outputs this:
AssertionError: assert
not_redirected(<django.http.response.HttpResponseRedirect object at
0x493da10>)
How would you generate a useful error message, bearing in mind that you
can't really access response._headers['location'][1] without first having
checked that the response is a redirect, which isn't the normal case?
Regarding magic in Pytest - yeah - there is some =)) But if think
deeper, inheritance and self is also magic, and Pytest just uses its own
magic (dependency injection) that is differs from OO magic =)
OO "magic" is well known and understood by most Python developers.
Pytest's magic is (a) largely undocumented (afaik) and (b) relies on
understanding of Python bytecode which is definitely not common knowledge.
Cheers, Chris.
--
Aptivate | http://www.aptivate.org | Phone: +44 1223 967 838
Citylife House, Sturton Street, Cambridge, CB1 2QF, UK
Aptivate is a not-for-profit company registered in England and Wales
with company number 04980791.
--
You received this message because you are subscribed to the Google Groups "Django
developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/alpine.DEB.2.02.1404061926030.7500%40lap-x201.
For more options, visit https://groups.google.com/d/optout.