Eric Holscher: Stubbing out tests for Django

Django Community - Thursday, November 13, 2008 @ 06:00 PM

I was having a conversation with Jacob tonight about testing in Django. He has shot down testmaker for being too specific for Django core, which I almost agree with, given my grandiose plans for it before the month is out. I'm quite okay with it staying a third party app for a little while longer.

However, that got us on the topic of testing, and I think it's interesting enough to post here to get some feedback and to tell people what's up. First we talked about trying to stub out some tests for people in the startapp command in Django. Like Rails does, except there is a really hard question about what to provide in that file. Should we provide a simple test that passes and makes people feel good about testing? Do we be evil and provide a test that fails, with assert False, 'Write some tests yo!'?

So the idea then progressed into perhaps having a command that can be called later in the process to stub out your tests files with real data. Perhaps stubbing a test for each view in your URLconf and each method in your Models or something like that.

We also talked about the possibility of adding a fifth part to the Django Intro Tutorial about testing. I think that this is a really great idea, and would help further the testing culture inside of the Django community. I volunteered to write the first draft of that document, so expect that to be posted to this blog sometime next week.

So I'm just kind of curious what people think is a good way to get testing integrated into the Django community better. I am trying to write some tools that will help people write tests, which would help them have tests :). But I think that there is a lot more that can be done to get people thinking about testing their applications.

Should we be encouraging people to be testing from when they start a new application in Django? If so, what should we put in the tests.py file when they create an application? Should we just stub out an empty tests.py file to remind them that they should be writing tests? Should we be pushing best practices from the beginning in that form, or giving people a builtin option to perhaps then stub it out later?

I think that the Django community is lacking in the testing realm these days, and I'm curious what we can be doing to get more people excited about testing. It's a great tool, and something that everyone should be doing. So I'd love to hear feedback or ideas about what people think can and should be done with regards to testing.

Ross Poulton : The Uswaretech Blog: Using Paypal with Django

Django Community - Thursday, November 13, 2008 @ 05:00 PM

A great article on using PayPal within Django. Currently for WhisperGifts I create an encrypted button, however this means the users' end-point is the PayPal website. Using the snippets linked to from this article, the user sees PayPal as a single step and the final end-point is on your own website, giving you more control over the checkout process.

Ross Poulton : Tony Hauber: Django Schedule, a pluggable calendar app for Django

Django Community - Thursday, November 13, 2008 @ 04:00 PM

Tony Hauber has put together a fantastic pluggable application for Django which allows you to manage events, including recurring events. This will be very useful for an upcoming project where I've hacked together my own recurring event framework that's absurdly messy. Recurring events are hard to get right, but it appears Tony has got it right here.

This Week in Django: This Week in Django 44

Django Community - Thursday, November 13, 2008 @ 04:00 PM

This Week in Django is a weekly podcast about all things Django.

This week we are privileged to talk with Django core developer Russell Keith-Magee. We also discuss a few source commits, some cool projects from the community, and a Tip of the Week.

Apologies to Kevin Fricovsky for failing to mention his tremendous support and contributions to the This Week in Django podcast and website. He says if it happens again we all get spankings. Be sure to check out his much talked about consulting company Monty Lounge Industries.

Please see the Show Notes below for all the pertinent information and links

Show Notes

TWiD Updates

  • We’re going to keep the poll up for one more week – Brought to you by Rob Hudson) Which 3rd party re-usable Django app would you most like to see in Django 1.1?
    a) django-notification
    b) django-tagging
    c) django-registration
    d) django-mailer
    e) django-openid
    f) none, 3rd party apps don’t belong in django proper
  • Getting on a schedule for releases – Every Thursday you can expect TWiD in your RSS feed.
  • Making a Django Uber-Community – Erich Holscher wrote an interesting post about the greater Django community, the urgency behind a proper content aggregator, and the possibilities of TWID leading that role. He also discussed some of the ideas spread around DjangoCon in regards to leveraging OpenId to consolidate and unify existing Django social/community site.

Interview – Russell Keith-Magee (5:35)

Russell read Physics and Computer Science as an undergraduate. His PhD
was also in computer science, looking at models of learning and
development in neural networks.

Russell became a core developer for Django in January 2006. He is
responsible for developing a number of major features in Django,
including the integrated test framework and the newforms media
framework. He has also mentored a Google Summer of Code project
to add aggregation functions to the Django ORM. Additionally, he is a
co-founder of Django Evolution, which was released publicly in
November 2007.

Russell is currently the Senior Software R&D Engineer with
Wotnews.com, a startup company providing business news monitoring
services. He was previously employed as a Senior Engineer at Calytrix
Technologies, an Australian defense contractor specializing in
simulation and exercise support services. He has been using Django
professionally on a daily basis for almost 2 years. He also has
extensive experience with Java, C++, and a little known simulation
protocol called HLA.

Tracking Trunk (1:00:51)

Community Catchup (1:08:28)

  • Django-Pendulum – A time logging application for Django-powered sites.You can have an unlimited number of projects to be able to categorize hours spent working on certain tasks. Each project can be activated/deactivated as necessary via the Django admin. Activities allow you to further categorize work done on particular tasks for each project.Pendulum can be configured to operate on several Django-powered sites. The period lengths can be configured as monthly or as a fixed-length period.
  • Django-Treebeard – Need to implement a tree structure using your django models? From the author “Django-treebeard is a library that implements efficient tree implementations for the Django Web Framework 1.0+. It includes 3 different tree implementations: Adjacency List, Materialized Path and Nested Sets. Each one has it’s own strength and weaknesses (see Benchmarks) but share the same API, so it’s easy to switch between implementations…uses Django Model Inheritance with abstract classes to let you define your own models.”
  • Lazy User Foreign Keys – Great post by our good friend Alex Gayner where he presents a very creative solution to implementing the common idiom of having a foreign key field to the User model. He wraps it up into a special field that makes it easy to access the user from the model instance.
  • django-test-utils – Eric Holscher announced his new project today django-test-utils. Eric admits there’s not a ton there yet, but what is there is quite interesting – Django Crawler. What it does is loops through all of your URLConfs and then provides some output based on arguments you provide like ”-t” for time your requests, ”-p” option allows you to drop into pdb on an error in your site, ”-s” alerts you when you have escaped HTML fragments in your templates, and ”-r” tells the crawler to store the response object for each site. This is just the beginning as Eric said, so we expect a lot more utility to come out of this project. great start!

Tip of the Week (1:30:53)

To use subqueries as a source for your in condition just pass in a QuerySet and refernece the query property.

ModelA.objects.filter(model__in=ModelB.objects.filter(some_filter=value).query)

Thank You! (1:34:59)

Bas van Oostveen: Added kcachegrind support to runprofileserver

Django Community - Thursday, November 13, 2008 @ 04:01 AM

Today i've added a new option to Django-Extension's runprofileserver to make like a bit easier for people who want to use KCacheGrind to profile Django.

This was sparked by profiling one of the scientific Python scripts i wrote for my research project. I wanted to see where (or at least if) i could squeeze a bit more performance out of it without resorting to ctypes/c-modules or weave. For more information about the latter see: http://www.scipy.org/PerformancePython

So i disabled Psyco [1] which confuses the profiler and started my program with the cProfiler enabled and started analyzing it's output.

you can do this easily by executing:

  $ python -m cProfiler ./my_application.py

From earlier encounters with profiling i found that KCacheGrind is a really awesome application for viewing and analyzing profile data. However Python's profiler module cannot directly save it's data in a format which is compatible with KCacheGrind. There are a few ways around this and one of them is a script called: lsprofcalltree.py [2].

It converts the profile information from cProfiler to something which is readable by KCacheGrind. Best of all you can use it as an in-place replacement for the Python interpreter so it's just as easy to use as the "$ python -m cProfiler" line.

Coming back to the runprofileserver I implemented lsprofcalltree directly into the extension command so you do not have to convert the output data by hand later. All you need to do now is enable the --kcachegrind option and all the profiler data is automatically saved in the KCacheGrind compatible format.

Example:

$ mkdir /tmp/my-profile-data
$ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data
Validating models...
0 errors found

Django version 1.0-post-release-SVN-SVN-unknown, using settings 'complete_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[13/Nov/2008 06:29:38] "GET / HTTP/1.1" 200 41107
[13/Nov/2008 06:29:39] "GET /site_media/base.css?743 HTTP/1.1" 200 17227
[13/Nov/2008 06:29:39] "GET /site_media/logo.png HTTP/1.1" 200 3474
[13/Nov/2008 06:29:39] "GET /site_media/jquery.js HTTP/1.1" 200 31033
[13/Nov/2008 06:29:39] "GET /site_media/heading.png HTTP/1.1" 200 247
[13/Nov/2008 06:29:39] "GET /site_media/base.js HTTP/1.1" 200 751
<ctrl-c>
$ kcachegrind /tmp/my-profile-data/root.12574391.592.prof

Here is a screenshot of how the above commands might look in KCacheGrind:

Screenshot

More information can be found at http://code.google.com/p/django-command-extensions/wiki/RunProfileServer

Have fun profiling :)

P.S. Having fancy tools is never a replacement for learning and knowing about what code actually does and what profile data represents !

[1]The Python Dynamic/JIT compiler - http://psyco.sf.net/
[2]http://codespeak.net/pypy/dist/pypy/tool/lsprofcalltree.py

Rails Envy Podcast - Episode #055: 11/12/2008

Rails Envy - Thursday, November 13, 2008 @ 01:26 AM

Episode 055. RubyConf wrap-up this episode. It's also the longest episode ever clocking in at 26 minutes of awesome.

Subscribe via iTunes - iTunes only link.
Download the podcast ~26:30 mins MP3.
Subscribe to feed via RSS by copying the link to your RSS Reader


The Rails Envy podcast is brought to you this week by Run Code Run by Relevance, Inc. Run code run is continuous integration done right. With Run code run you get constant feedback on the health of your project and zero config setup with github integration.

The Rails Envy podcast is also brought to you this week by Cloudspace. Cloudspace specializes in designing & building web applications using agile software development in Ruby on Rails and PHP/MySQL frameworks.


Call Us! You can reach the podcast voicemail line at 407 409-8440.


Rails Envy Podcast - Episode #055: 11/12/2008

Rails Envy - Thursday, November 13, 2008 @ 01:26 AM

Episode 055. RubyConf wrap-up this episode. It's also the longest episode ever clocking in at 26 minutes of awesome.

Subscribe via iTunes - iTunes only link.
Download the podcast ~26:30 mins MP3.
Subscribe to feed via RSS by copying the link to your RSS Reader


The Rails Envy podcast is brought to you this week by Run Code Run by Relevance, Inc. Run code run is continuous integration done right. With Run code run you get constant feedback on the health of your project and zero config setup with github integration.

The Rails Envy podcast is also brought to you this week by Cloudspace. Cloudspace specializes in designing & building web applications using agile software development in Ruby on Rails and PHP/MySQL frameworks.


Call Us! You can reach the podcast voicemail line at 407 409-8440.