django-testing

Django : Testing if the page has redirected to the desired url

点点圈 提交于 2019-11-27 14:55:58
In my django app, I have an authentication system. So, If I do not log in and try to access some profile's personal info, I get redirected to a login page. Now, I need to write a test case for this. The responses from the browsers I get is : GET /myprofile/data/some_id/ HTTP/1.1 302 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 301 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 200 6533 How do I write my test ? This what I have so far: self.client.login(user="user", password="passwd") response = self.client.get('/myprofile/data/some_id/') self.assertEqual(response

Detect django testing mode

拜拜、爱过 提交于 2019-11-27 14:18:12
问题 I'm writing a reusable django app and I need to ensure that its models are only sync'ed when the app is in test mode. I've tried to use a custom DjangoTestRunner, but I found no examples of how to do that (the documentation only shows how to define a custom test runner). So, does anybody have an idea of how to do it? EDIT Here's how I'm doing it: #in settings.py import sys TEST = 'test' in sys.argv Hope it helps. 回答1: I think the answer provided here https://stackoverflow.com/a/7651002/465673

IntegrityError when loading fixture during django testing

孤人 提交于 2019-11-27 12:11:05
问题 I'm loading a fixture created with dumpdata, and getting the following exception: Problem installing fixture 'db_dump.json': Traceback (most recent call last): File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle obj.save(using=using) File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save models.Model.save_base(self.object, using=using, raw=True) File "/usr/lib/python2.6/site-packages/django/db/models/base

Django Reverse with arguments '()' and keyword arguments '{}' not found

不打扰是莪最后的温柔 提交于 2019-11-27 10:42:30
Hi I have an infuriating problem. I have a url pattern like this: # mproject/myapp.urls.py url(r'^project/(?P<project_id>\d+)/$','user_profile.views.EditProject',name='edit_project'), it works fine in the browser but for testing, when I do this in the shell: from django.test import Client from django.core.urlresolvers import reverse client= Client() response = client.get(reverse('edit_project'), project_id=4) I get the dreaded: NoReverseMatch: Reverse for 'edit_project' with arguments '()' and keyword arguments '{}' not found. What am I missing here? You have to specify project_id : reverse(

What are the best practices for testing “different layers” in Django? [closed]

强颜欢笑 提交于 2019-11-27 09:05:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm NOT new to testing, but got really confused with the mess of recommendations for testing different layers in Django. Some

Django test to use existing database

核能气质少年 提交于 2019-11-27 02:09:38
问题 I'm having a hard time customizing the test database setup behavior. I would like to achieve the following: The test suites need to use an existing database The test suite shouldn't erase or recreate the database instead load the data from a mysql dump Since the db is populated from a dump, no fixtures should be loaded Upon finishing tests the database shouldn't be destroyed I'm having a hard time getting the testsuiterunner to bypass creation. 回答1: Fast forward to 2016 and the ability to

Django's self.client.login(…) does not work in unit tests

断了今生、忘了曾经 提交于 2019-11-26 22:20:20
问题 I have created users for my unit tests in two ways: 1) Create a fixture for "auth.user" that looks roughly like this: { "pk": 1, "model": "auth.user", "fields": { "username": "homer", "is_active": 1, "password": "sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2", ... } } I've left out the seemingly unimportant parts. 2) Use 'create_user' in the setUp function (although I'd rather keep everything in my fixtures class): def setUp(self): User.objects.create_user('homer', 'ho...@simpson.net',

Django : Testing if the page has redirected to the desired url

给你一囗甜甜゛ 提交于 2019-11-26 16:57:42
问题 In my django app, I have an authentication system. So, If I do not log in and try to access some profile's personal info, I get redirected to a login page. Now, I need to write a test case for this. The responses from the browsers I get is : GET /myprofile/data/some_id/ HTTP/1.1 302 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 301 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 200 6533 How do I write my test ? This what I have so far: self.client.login(user="user",