testing a django package

42 views
Skip to first unread message

Jonas Geiregat

unread,
Sep 10, 2012, 11:23:23 AM9/10/12
to django...@googlegroups.com
Hello,

I've created a simple reusable django package. This package basically consists out of a views.py file, with some helper functions.

I want to write some tests for what's in this file. This file contains a mixin , so I probably can't test it directly.

What's the best way to test a django package ?

Do I need to include some kind of example project that, uses my mixin and has tests files just as one would test a normal django application ?

I've tried googling but, there's little about this. Most of the things I've found where related to testing actual django applications.

Any help is appreciated.

Jonas


Thomas Orozco

unread,
Sep 11, 2012, 3:41:37 AM9/11/12
to django...@googlegroups.com

Is it a view mix in?

It's a bit difficult to tell you much without more information.

A few you things that you may find useful or not for testing :

.  Test cases can override settings such as the urlconf
.  There's a test client to test views
.  Class based views can sometimes be tested without any of the former by just testing the methods in them
.   You can always use mocking library
.  Tests are basically just python code with a lot of asserts, you can always add viewed in them
.  You can use fixtures for test data if you see fit

Hope this helps!

Thomas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Jonas Geiregat

unread,
Sep 11, 2012, 12:19:45 PM9/11/12
to django...@googlegroups.com

Is it a view mix in?

The package actually already changed from containing a view mix in to actual views that should be subclassed by the users using the package.

It's a bit difficult to tell you much without more information.

For example, a view derived from FormView. Like I said the user should subclass this view again as you would normally do with CBV.

Currently the package is just a package/directory with views.py, models.py, urls.py and of course __init__.py to make it a module.

It's hard to test views without having an actual django project to test it again, I think.  Correct me if I'm wrong here.
So I created an example django project that is using this package in the directory below the package, thus the directory containing the README, setup.py etc .. files.

The example app will hold a tests.py file which will contain my tests which will be testing the actual functionality of my views.

Basically I'm asking do I need an actual django project to test just a views.py file or can I just write some tests without a containing django project to test a views.py file.
And if so, I'm kinda lost on how you would start on such a task.

Jonas Geiregat

unread,
Sep 12, 2012, 5:16:04 PM9/12/12
to django...@googlegroups.com

I can now show you what I really would like to write tests for.


The code I would like to test is in ajax_forms/views.py mainly the AjaxFormView and the AjaxModelFormView.

Any help is appreciated!

Thomas Orozco

unread,
Sep 12, 2012, 5:24:30 PM9/12/12
to django...@googlegroups.com
I think you could create an urls.py file for your tests and attach
your views there. Then use the test client to direct requests to those
test URLs and assert that the response is what you expect.

Now, this is probably not going to be practical to test your jQuery
components, but you might want to ask on a JS-focused list regarding
unittesting JS for this purpose.

Thomas

2012/9/12 Jonas Geiregat <jo...@geiregat.org>:

Jonas Geiregat

unread,
Sep 12, 2012, 5:35:34 PM9/12/12
to django...@googlegroups.com



> I think you could create an urls.py file for your tests and attach
> your views there. Then use the test client to direct requests to those
> test URLs and assert that the response is what you expect.

I didn't know that could work.
I think I'll also need to create views that extend these views that I want to test.
Since they need actual implementation, I think, such as template_name and model etc ..

How would the test client then now where to look for these views, since there will be some reversing involved I think.

self.client.get(reverse("ajax_form_view"))

I don't see how the client will find my urls.py file and without the broader, actual django application as context ?

Thomas Orozco

unread,
Sep 12, 2012, 5:41:20 PM9/12/12
to django...@googlegroups.com
Actually, Django TestCases can override the URLConf, which allows you to have a given URL for a given view in your test:

from django.test import TestCase

class MyTestCase(TestCase):
    urls = 'mypackage.tests.urls'

    def test_something():
        # Do stuff...


From there, you can *totally* just create a tests/views.py file where you add your test view with the actual implementation you're missing, and add it in your tests/urls.py file.

And everything will be fine : )

Jonas Geiregat

unread,
Sep 13, 2012, 3:03:13 PM9/13/12
to django...@googlegroups.com
Thanks you for your help. 

I understand now how I should setup testing for a django package. I'm using django-registration as an example , which works a similar way.

I have a weird problem at hand.
I'm creating a django package and would like to provide some test cases for it.
The package is named ajax_forms and the full path is part of my PYTHONPATH. This directory also contains a tests directory (with the init.py file) a tests.py file (containing the tests) and a urls.py file.
Now I want to run the tests.

The ajax_forms parent directory has an example project setup with ajax_forms in INSTALLED_APPS.

But I can't seem to let the tests run:

 >> ./manage.py test ajax_forms
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...

No tests are found so I try to be more specific:

 >> ./manage.py test ajax_forms.tests
....
raise ValueError("Test label '%s' does not refer to a test" % label)
ValueError: Test label 'ajax_forms.tests' does not refer to a test
(ajax-validation)

Even ./manage.py ajax_forms.tests.tests.AjaxFormViewTest will not find the tests and gives the same error as above.

But trying to import the test case from the ./manage.py shell works fine

In [2]: from ajax_forms.tests.tests import AjaxFormViewTest

I'm kinda clueless here, on what's going on.

Jonas Geiregat

unread,
Sep 13, 2012, 3:13:55 PM9/13/12
to django...@googlegroups.com
Found the answer:

I need to put from tests import * in the tests/__init__.py file 

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/1Fkl5iGxg74J.
Reply all
Reply to author
Forward
0 new messages