django-testing

Using Basic HTTP access authentication in Django testing framework

和自甴很熟 提交于 2019-11-28 19:33:49
问题 For some of my Django views I've created a decorator that performs Basic HTTP access authentication. However, while writing test cases in Django, it took me a while to work out how to authenticate to the view. Here's how I did it. I hope somebody finds this useful. 回答1: Here's how I did it: from django.test import Client import base64 auth_headers = { 'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode('username:password'), } c = Client() response = c.get('/my-protected-url/', **auth_headers)

IntegrityError when loading fixture during django testing

妖精的绣舞 提交于 2019-11-28 19:16:30
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.py", line 526, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values) File "/usr

How do I modify the session in the Django test framework

风流意气都作罢 提交于 2019-11-28 19:12:09
My site allows individuals to contribute content in the absence of being logged in by creating a User based on the current session_key I would like to setup a test for my view, but it seems that it is not possible to modify the request.session: I'd like to do this: from django.contrib.sessions.models import Session s = Session() s.expire_date = '2010-12-05' s.session_key = 'my_session_key' s.save() self.client.session = s response = self.client.get('/myview/') But I get the error: AttributeError: can't set attribute Thoughts on how to modify the client session before making get requests? I

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

送分小仙女□ 提交于 2019-11-28 15:12:47
I'm NOT new to testing, but got really confused with the mess of recommendations for testing different layers in Django. Some recommend (and they are right) to avoid Doctests in the model as they are not maintainable... Others say don't use fixtures , as they are less flexible than helper functions , for instance.. There are also two groups of people who fight for using Mock objects. The first group believe in using Mock and isolating the rest of the system, while another group prefer to Stop Mocking and start testing .. All I have mentioned above, were mostly in regards to testing models.

How should I write tests for Forms in Django?

醉酒当歌 提交于 2019-11-28 14:34:38
问题 I'd like to simulate requests to my views in Django when I'm writing tests. This is mainly to test the forms. Here's a snippet of a simple test request: from django.tests import TestCase class MyTests(TestCase): def test_forms(self): response = self.client.post("/my/form/", {'something':'something'}) self.assertEqual(response.status_code, 200) # we get our page back with an error The page always returns a response of 200 whether there's an form error or not. How can I check that my Form

django test app error - Got an error creating the test database: permission denied to create database

≯℡__Kan透↙ 提交于 2019-11-28 13:45:26
问题 When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command): python manage.py test appname I get this error: Creating test database for alias 'default'... Got an error creating the test database: permission denied to create database Type 'yes' if you would like to try deleting the test database 'test_finance', or 'no' to cancel syncdb command seems to work. My database settings in settings.py: DATABASES = { 'default': { 'ENGINE

Django test to use existing database

断了今生、忘了曾经 提交于 2019-11-28 08:21:11
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. Fast forward to 2016 and the ability to retain the database between tests has been built into django. It's available in the form of the --keep flag to

How do I test Django QuerySets are equal?

孤人 提交于 2019-11-27 23:01:11
问题 I am trying to test my Django views. This view passes a QuerySet to the template: def merchant_home(request, slug): merchant = Merchant.objects.get(slug=slug) product_list = merchant.products.all() return render_to_response('merchant_home.html', {'merchant': merchant, 'product_list': product_list}, context_instance=RequestContext(request)) and test: def test(self): "Merchant home view should send merchant and merchant products to the template" merchant = Merchant.objects.create(name='test

Django coverage test for URLs 0%, why?

给你一囗甜甜゛ 提交于 2019-11-27 18:58:25
问题 Using Django Nose. I have tests for my URL's but coverage is still giving me 0% for URLs, why? python manage.py test profiles This is my coverage: Name Stmts Miss Cover Missing ---------------------------------------------------------------- profiles 0 0 100% profiles.migrations 0 0 100% profiles.migrations.0001_initial 6 0 100% profiles.models 0 0 100% profiles.urls 4 4 0% 1-9 ---------------------------------------------------------------- TOTAL 10 4 60% ------------------------------------

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

℡╲_俬逩灬. 提交于 2019-11-27 17:47:21
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', 'simpson') Note that the password is simpson in both cases. I've verified that this info is correctly