What is the best way to get started with unit testing in django?
-
I've Googled around for the best resources on introductions to unit testing in django, and haven't found much that is helpful. Lots of the examples are too simple, and it seems that you need a lot of effort into setting up fixtures, or figuring out how to mock requests that come close to approximating what a user would actually do. Right now I'm using Django's TestCase. For someone who is new to unit testing in django, do you have good resources or recommendations beyond a first google? How do you simulate requests, logged in users, ..., and what kind of things would you validate? Do you need fixtures do anything useful? How close can you get to making test requests like real requests?
-
Answer:
I think you may be confusing unit-tests with integration-tests. unit-tests are meant to evaluate the views as functions/callables in a fully-procedural way, that is: give an input, compare the output with the expected result. if you want to simulate user behavior you should do integration, that is what Selenium is for. regarding login, Django's documentation shows you a way to login users from within the tests but for this you need users on the database and that's what fixtures are for; the best to do that is to run the application with an empty database, create the tests users using your normal registration process and then dump that into a fixture (with the dumpdata command), you can tell your tests to use that fixture and then you'll have your users for the login. the problem with fixtures is that they need to be updated when the models change so there's some apps that create test data on the fly to deal with this, there's factory_boy and django_factory and maybe others.
Michael Cetrulo at Quora Visit the source
Other answers
After struggling to understand how and whats of testing. I think the best approach is to start off with the Unit Test(Most of them seem to be using that.) Django docs have some basic stuff to get started with https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/ Here is theĀ http://blip.tv/djangocon/token-testing-talk-3059472 http://blip.tv/djangocon/token-testing-talk-3059472To get a feel of it http://gurovich.com/site/2010/03/20/test-driven-development-with-django-unit-testing/. Sort of some serious stuff (U know the bank number :P) And then probably some of the Mozilla github repos, or any open source projects with test cases. So I am at the step 2 after a night struggle.Hope you find it useful.
Anup Kalburgi
Related Q & A:
- What's the best way to get into the Graphic Design industry?Best solution by Yahoo! Answers
- What is the best way to get from Denver to Fort Collins?Best solution by Yahoo! Answers
- What is the best way to get a job on cruise ships without spending money?Best solution by cruiseserver.net
- What is the best way to get website traffic?Best solution by Yahoo! Answers
- What's the best way to get a job in a restaurant?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.