Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django testing tips [closed]

In the spirit of this question, I would like to know if anyone has any tips on creating a useful and "complete" test suite (can a test suite ever be "complete"?) for a Django webapp.

My situation: I've knocked out a prototype and am now working on adding some regression testing. I personally use django-webtest for most of my tests with some URL testing using the Django test client.

II do not feel comfortable with my test suite at all. I am far from a testing pro so trying to improve on that end. Any tips---whether applicable in my situation or not---would be greatly appreciated.

like image 236
Belmin Fernandez Avatar asked Dec 15 '10 00:12

Belmin Fernandez


People also ask

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.

How do I test coverage in Django?

With Django's Test Runner. If you're using manage.py test , you need to change the way you run it. You need to wrap it with three coverage commands like so: $ coverage erase # Remove any coverage data from previous runs $ coverage run manage.py test # Run the full test suite Creating test database for alias 'default'.. ...

What should you test in Django?

As discussed above, we should test anything that is part of our design or that is defined by code that we have written, but not libraries/code that is already tested by Django or the Python development team. For example, consider the Author model below.

What is RequestFactory in Django?

The request factory class RequestFactory. The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.


2 Answers

I would recommend reading Django 1.1 Testing and Debugging by Karen M. Tracey. The first five chapters cover testing in Django. Specifically, you should look at Chapter 5 which discusses integrating other test tools. Below is an excerpt of what Chapter 5 covers:

In this chapter, we:

  • Learned what hooks Django provides for adding test functions
  • Saw an example of how these hooks can be used, specifically in the case of adding code coverage reporting
  • Also explored an example where using these hooks was not necessary—when integrating the use of the twill test tool into our Django test cases

Here are links to some of the tools that Karen Tracey discusses in chapter 5 of her book:

  • Ned Batchelder's coverage module
  • Twill

Lettuce

You may also want to check out Lettuce. From the website:

Lettuce is a very simple BDD tool based on the Cucumber.

The Lettuce documentation also has a section on integrating Lettuce with Django.

like image 128
Matthew Rankin Avatar answered Oct 17 '22 17:10

Matthew Rankin


Testing provides the answers to (at least ) 4 questions.

  1. Is my implementation correct? Does the app match the documented or at least mental image of how it is supposed to behave.

  2. Did my latest change break anything?

  3. Is my app secure? From both innocent users and devious people.

  4. Is my app's performance sufficient?

For #1, there needs to be at least one test per "feature" and probably many for major features. It is very easy to make errors of omission here if you are both the developer and the test developer.

For #2 starting out with the discipline of writing the test suites along with the code (and faithfully running it) is key.

For #3, Make sure that URLs accessed outside of normal program directing operations behave properly with respect to permissions. You probably don't want one user to be able to modify or even another user' info but if they can just type in ../user/505 and get to everything there that's probably a problem. I'm sure there is a lot of other things that should be tested here, so other people chime in here please.

Testing performance and scaling robustness for an app with a tremendous amount of traffic isn't something I know much about.

Looking at the test cases for Django itself provide a sense of the granularity of what should be tested.Django trunk tests

Django docs have a good article on testing: search the docs for testing.

like image 42
Vicky T Avatar answered Oct 17 '22 18:10

Vicky T