-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
iQIcBAEBCAAGBQJWSkFSAAoJEC0ft5FqUuEh0IsP/jFahlxvn4gc6c/l6WoVyGXG
G8Cmfv/Wco45G5KQuMun5MpJSWAbWR5KM3oWhuEsQmWPp5vgSUvbg//J1RfzpYJ2
TjIfKWHls32sYug6c0xASSPB4ESIJCC3r5b5emI4fpmlzkIMJdKoViZjita/mxP4
ubm1u6MCRHZU7qKwx6ByYxq57QMwGD87Kn1aLfYrO5Dxvmq7saoReWfyQ5Xo/ahQ
pmpX/I55OJr+M8Ki1Nv/2lYwIQRzpZqA9qB74BePWzhBw5MHrFC5rxFBegBEjyIf
Cv0tci6nzc0Gdax2WqRJ3Ax5i6KQ707Lu9VgnNL/BpG+ke+7ihhFHlLZ4GWZ8eNH
oUpb2XGH8ofL0yCF9O74xbdpZ2WXoW3tNIuiH9aq0lPS7QcX2oq4mfURE+1eLsw8
O3b3P09/1DHLhLPjCOWRiMsPJKs+3Y1RShfzfulyZWZSc4YRHOrwqZkCvBqO42tP
sRNf/CaHapiKcg1bkuKhbaKc76Qw/HQ6e2JoPMwkDjXuJRKOJ/HU26dEo8Awx/RT
DxLU+SNaHbe1uh4lrf5QoW5gM9I2oKbO6jGAmFSuMxg+vVtIeK82OgllvdcOCVoZ
61NAjqdcghW21SYvg6f36l8RIlJTUIu2ao2lWBIKrQ8EdP/jQnWE+rbLiUJ2Eug2
1O10yFygZxxm5Lq8w1/b
=oLZd
-----END PGP SIGNATURE-----
Hi Tim,
On 11/16/2015 01:35 PM, Tim Chase wrote:
> Are there best practices for writing tests where every case has a
> "not logged in" and a "logged in" pairing? I'm finding myself writing
> too much redundant test-code where the only difference is whether the
> user is anonymous (and thus the test should fail) or logged in with
> appropriate rights (and thus the test should pass).
>
> For context, this is being run within Django-WebTest as recommended
> to me by Carl Meyer in case it has features I haven't yet encountered
> that would help me.
>
> Are there best practices to keep these sorts of tests DRY?
Now I'm going to recommend another testing tool: py.test :-)
I handle this type of situation by using py.test parametrization.
Usually I have a bunch of views that are all login-required, so I just
write a single test to check that a view returns redirect-to-login if
accessed anonymously, and then parametrize that test over all the urls
it should apply to. It looks something like this (parametrizing over
just two views):
@pytest.mark.parametrize(
'urlname,urlkwargs',
[('oneview', {}), ('otherview', {'id': 999})],
)
def test_login_required(client, urlname, urlkwargs):
url = reverse(urlname, kwargs=urlkwargs)
resp = client.get(url)
assert utils.redirects_to(resp) == reverse('accounts_login')
I use url-reversing in my tests; the parametrization would be slightly
simpler if you just used raw URLs. The `client` kwarg is a py.test
fixture that returns a WebTest client instance. `utils.redirects_to` is
just a helper that pulls out the `Location` header from the response,
and strips out everything but the path portion.
HTH,
Carl
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/564A414F.4040302%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment