django-testing

Adding extra filter to polls urls.py causes tests to fail

谁都会走 提交于 2019-12-12 19:45:16
问题 Following the tutorial at djangoproject, I have tried to have urls.py filter out the polls with no choices with the urlpattern below. urlpatterns = patterns('', url(r'^$', ListView.as_view( queryset=Poll.objects.filter(choice__choice_text__isnull=False) \ .filter(pub_date__lte=timezone.now) \ .order_by('-pub_date')[:5], context_object_name='latest_polls', template_name='polls/index.html'), name='index'), url(r'^(?P<pk>\d+)/$', DetailView.as_view( queryset=Poll.objects.filter(choice__choice

Django Testing - I only want one of my databases created - how to specify

久未见 提交于 2019-12-12 14:43:08
问题 So in my settings.py I specified two database connections (see below). But when I run tests I only want Django to create the 'default' database. Is there any way to do this? I tried adding the TEST_CREATE: False option but I guess that's only for Oracle for some reason? My settings.py snippet: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'App1', # Or path to database file if using sqlite3.

Configure Django to find all doctests in all modules?

≡放荡痞女 提交于 2019-12-12 07:48:35
问题 If I run the following command: >python manage.py test Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so: #tests.py from myapp.module1 import _function1, _function2 __test__ = { "_function1": _function1, "_function2": _function2 } If I want to include more doctests, is there an easier way than enumerating them all in this dictionary?

Write test for views containing os.remove in django

自作多情 提交于 2019-12-12 01:27:42
问题 I have a function based view function in django that receives an ID from a model, retrieve a file address and delete it using os.remove image = Images.objects.get(id=image_id) os.remove(image.file) the image_id is valid and is a part of my fixture. what's the best way to write a test for this view, without manually creating a file each time I'm testing the code? Is there a way to change the behavior of os.remove function for test? 回答1: Yes. It's called mocking, and there is a Python library

Django test client POST command not registering through 301 redirect

痞子三分冷 提交于 2019-12-11 13:14:04
问题 I'm writing Django tests for a live Heroku server, and am having trouble getting Django to recognize a POST request through a redirect. On my test server, things work fine: views.py def detect_post(request): """ Detect whether this is a POST or a GET request. """ if request.method == 'POST': return HttpResponse( json.dumps({"POST request": "POST request detected"}), content_type="application/json" ) # If this is a GET request, return an error else: return HttpResponse( json.dumps({"Access

Django testing - Multiple queries when externally modifying the data. Cache Issue?

孤街浪徒 提交于 2019-12-11 08:36:08
问题 I have a legacy application which is (currently) using Django to effectively display data. A sample of one of my working tests looks like this. def test_add_property_value(self): """Test set / get a value""" # This will do some external process which occcurs to the db. pm = Pm(mysql_db='test_bugs') tree = pm.add_release_tree() prop_type, pmvalue = ("string", "Funny Business") pmproperty = "%s_%s_basic" % (tree[0].name, prop_type) pm.add_property_definition(pmproperty, prop_type=prop_type) pm

Django test client Response contains empty list of templates?

落爺英雄遲暮 提交于 2019-12-10 15:36:33
问题 According to the Django testing docs, the Django client Response object contains 'templates', which is: "A list of Template instances used to render the final content, in the order they were rendered. For each template in the list, use template.name to get the template's file name, if the template was loaded from a file. (The name is a string such as 'admin/index.html'.)" However, I am getting an empty list of templates, even though I am confident that a template was rendered. from django

Python mock, django and requests

*爱你&永不变心* 提交于 2019-12-10 12:39:29
问题 So, I've just started using mock with a Django project. I'm trying to mock out part of a view which makes a request to a remote API to confirm a subscription request was genuine (a form of verification as per the spec I'm working to). What I have resembles: class SubscriptionView(View): def post(self, request, **kwargs): remote_url = request.POST.get('remote_url') if remote_url: response = requests.get(remote_url, params={'verify': 'hello'}) if response.status_code != 200: return HttpResponse

How to add authentication token in header of `APIClient` in `django rest_framework test`

岁酱吖の 提交于 2019-12-08 21:36:23
问题 I am using oauth2_provider for my rest_framework . I am trying to write test case for my api. I have obtained an access token. But I am not able to authenticate user using access token in APIClient I am looking to get this curl command work with APIClient . curl -H "Authorization: Bearer <your_access_token>" http://localhost:8000/api/v1/users/current/ I have tried client.get('/api/v1/users/current/', headers={'Authorization': 'Bearer {}'.format(self.access_token)}) and client.credentials(HTTP

Loading SQL dump before running Django tests

浪子不回头ぞ 提交于 2019-12-08 16:01:15
问题 I have a fairly complex Django project which makes it hard/impossible to use fixtures for loading data. What I would like to do is to load a database dump from the production database server after all tables has bene created by the testrunner and before the actual tests start running. I've tried various "magic" in MyTestCase.setUp(), but with no luck. Any suggestions would be most welcome. Thanks. 回答1: You may need to look into defining a custom test runner. There's some info here: https:/