django-testing

Specify Django Test Database names in settings.py

前提是你 提交于 2019-11-30 07:46:42
问题 I'm specifying the databases using a python object: DATABASES = { 'default':{ 'ENGINE':'mysql', 'NAME':'testsqldb', 'USER':'<username>', 'PASSWORD':'<password>', }, 'dynamic_data':{ 'ENGINE': 'sqlite3', 'NAME':'', 'USER':'', 'PASSWORD':'' }, } How can I specify the name of my test database? I've been trying to use TEST_NAME = 'auto_tests' in the settings.py file. However, when I run python manage.py tests <app_name> I get the following message: Creating test database 'default'... Got an error

How do I modify the session in the Django test framework

北城余情 提交于 2019-11-30 06:32:24
问题 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:

Using Basic HTTP access authentication in Django testing framework

可紊 提交于 2019-11-29 22:50:13
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. Humphrey 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) Note: You will also need to create a user. In your Django TestCase you can update the client

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

孤街浪徒 提交于 2019-11-29 18:49:54
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.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle

Broken Pipe Error while running django-test with selenium

最后都变了- 提交于 2019-11-29 09:27:46
问题 while running django tests with selenium (no remote, no xvfb), I always get the following exception: Creating test database for alias 'default'... Traceback (most recent call last): File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run self.finish_response() File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response self.write(data) File "/usr/lib/python2.7/wsgiref/handlers.py", line 210, in write self.send_headers() File "/usr/lib/python2.7/wsgiref/handlers.py",

How do I test Django QuerySets are equal?

六月ゝ 毕业季﹏ 提交于 2019-11-29 05:29: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 merchant') product = Product.objects.create(name='test product', price=100.00) merchant.products.add

Specify Django Test Database names in settings.py

守給你的承諾、 提交于 2019-11-29 05:27:36
I'm specifying the databases using a python object: DATABASES = { 'default':{ 'ENGINE':'mysql', 'NAME':'testsqldb', 'USER':'<username>', 'PASSWORD':'<password>', }, 'dynamic_data':{ 'ENGINE': 'sqlite3', 'NAME':'', 'USER':'', 'PASSWORD':'' }, } How can I specify the name of my test database? I've been trying to use TEST_NAME = 'auto_tests' in the settings.py file. However, when I run python manage.py tests <app_name> I get the following message: Creating test database 'default'... Got an error creating the test database: (1007, "Can't create database 'test_testsqldb'; database exists") Type

Django test FileField using test fixtures

痴心易碎 提交于 2019-11-28 22:27:41
问题 I'm trying to build tests for some models that have a FileField. The model looks like this: class SolutionFile(models.Model): ''' A file from a solution. ''' solution = models.ForeignKey(Solution) file = models.FileField(upload_to=make_solution_file_path) I have encountered two problems: When saving data to a fixture using ./manage.py dumpdata , the file contents are not saved, only the file name is saved into the fixture. While I find this to be the expected behavior as the file contents are

Detect django testing mode

故事扮演 提交于 2019-11-28 22:20:38
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. jjmaestro I think the answer provided here https://stackoverflow.com/a/7651002/465673 is a much cleaner way of doing it: Put this in your settings.py: import sys TESTING = sys.argv[1

How can I unit test django messages?

别等时光非礼了梦想. 提交于 2019-11-28 21:54:36
问题 In my django application, I'm trying to write a unit test that performs an action and then checks the messages in the response. As far as I can tell, there is no nice way of doing this. I'm using the CookieStorage storage method, and I'd like to do something similar to the following: response = self.client.post('/do-something/', follow=True) self.assertEquals(response.context['messages'][0], "fail.") The problem is, all I get back is a print response.context['messages'] <django.contrib