Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Django unit testing [closed]

I'm trying to get caught up on unit testing, and I've looked over a few books - Debugging Django, Web Dev. with Django, and the official docs, but none seem to cover unit testing thoroughly enough for me. I'm also not an expert in Python web development, so maybe that's why.

What I'm looking for is something that starts at an intermediate level of python skill/knowledge and covers Django unit testing from scratch, with a few good real-world examples.

Any recommendations on such resources? Much appreciated.

like image 670
George K. Avatar asked Dec 04 '25 06:12

George K.


2 Answers

Reading

  • Test-Driven Django (by Harry Percival) seems great to start with.
  • article testing django signals is also useful, many people have been doing signal testing wrong.

Code

  • simple unit tests that were made for TDD from django-rules-light,
  • ugly selenium tests from django-autocomplete-light, if your selenium tests look like this then you've done it wrong (oh I'll redo them all right, one day ...)
  • interesting pattern in django-autocomplete-light: look how AutocompleteTestCase is the parent for other tests ie. this one has no actual test code, it just generates data for the AutocompleteTestCase.
  • good selenium tests from django-session-security. I'm happy with this one, not only is it readable, but the patterns used there allows the tests to run correctly even on very slow machines (ie. travis-ci free plan). Pay particular attention to this change which was the key, moving from a wait for X seconds, then see if Y happened pattern to ensure that Y happened within X seconds pattern. This IMHO makes better selenium tests.
  • ghost.py test example, but I think there was some bugs in my setup so I ended not using it, sounds worth trying anyway.

I'm sure you can find more on google/github/bitbucket/etc ...

Useful apps

djangopackages has quite some useful "testing tool" apps.

like image 95
3 revs, 2 users 97%jpic Avatar answered Dec 06 '25 23:12

3 revs, 2 users 97%jpic


I've read quite a few Django books and Python books and you're right, none of them really gets the job done. The best material I have come across to date is Carl Meyer's Talk on Testing and Django.

like image 43
super9 Avatar answered Dec 06 '25 22:12

super9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!