12

I am thinking of creating some subclassed Django model fields and distributing them as a package on PyPI. I like to write unit tests for my code (à la TDD), but I'm a bit puzzled as to how I'd write tests for this particular library.

The first thought that came to my mind was to create a Django project that makes use of my subclasses and just use the Django test tools, but that doesn't seem very elegant at all. There's got to be a better way!

Is there a method of somehow bootstrapping Django for this type of thing? I'd appreciate someone pointing me in the right direction. Thanks!

arussell84
  • 2,443
  • 17
  • 18
  • 1
    Why does there have to be a "better way"? Seems to me that if you're writing a library for Django, it makes perfect sense to use Django's testing interface. – Chris Pratt Feb 27 '12 at 22:10
  • @ChrisPratt It involves writing a **lot** of code that's not related to just _testing the library_. Django projects usually themselves require unit tests, so if I took this approach, I'd probably end up in essence writing unit tests for my unit tests. Not ideal! – arussell84 Feb 27 '12 at 22:23
  • 1
    No you wouldn't. You don't have to test anything that is not in your application. Django has its own tests for all it's stuff. Just test what's unique to your app, and rely on the Django stuff to "just work". It's not your responsibility to assure that. – Chris Pratt Feb 27 '12 at 22:25
  • @ChrisPratt No, not testing the Django codebase itself... There's a reason the Django testing interface exists, and that's to test a Django application. I don't _want_ to test a Django application. I want to test a Django model field subclass. – arussell84 Feb 27 '12 at 23:06
  • 1
    Which you'll create inside a Django application... Sheesh, it's all semantics at this point, but there's nothing special about Django's testing infrastructure. It's just unittest2 with a few addins to make testing Django stuff easier. If you're writing something *for* Django, you can assume Django is available, and therefore use the additions. You can always just import unittest2 directly, of course, but testing works the same either way. – Chris Pratt Feb 28 '12 at 00:43
  • 1
    @ChrisPratt Perhaps we are arguing about semantics, but you can't assume that I know enough about Django's internals to know that (I don't!). Basically I don't want to have to include a manage.py, urls.py, settings.py just for a test suite. It just doesn't seem like it should be necessary. My question is whether or not this is possible. I don't care whether you call it a Django application or not. I also don't know why you're giving me such a hard time when lazerscience already gave me a much more helpful answer 3 hours ago. – arussell84 Feb 28 '12 at 02:02

1 Answers1

12

Django itself comes with some tests for field-subclassing; the tests have their own models.py where the custom fields are used. You should get the best impression when you have a look at the actual code yourself!

Addition: To have the models defined in your test package being discovered by django you will have to add your yourapp.test package to INSTALLED_APPS. Django itself has a built-in mechanism for its own tests to be automatically discovered and added to INSTALLED_APPS.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • Pretty sure this is exactly what I'm looking for. I'll try it out and accept your answer after I confirm it. Thanks! – arussell84 Feb 27 '12 at 23:17
  • Sorry it took me so long to get back to this. I've pretty much lost interest in Django for the time being, but I wanted to at least give this a quick try. Turns out I don't think I can accomplish what I wanted. In order to create a subclassed field, I do need an entire Django project. Otherwise, there are no settings for the field to know where to save to, etc.! Anyway, this is as close to an answer as I got, so I'm marking it as such. – arussell84 Nov 10 '12 at 22:29
  • 4
    FYI, the link referred to by "actual code" has moved; it appears that it can now be found at https://github.com/django/django/blob/master/tests/field_subclassing/tests.py. – rmehlinger Oct 31 '13 at 23:12