django-tests

Unit tests fail to load with Django in Visual Studio

余生颓废 提交于 2019-12-23 03:18:18
问题 I am using Django 1.10.5 with Visual Studio 2015. My project is running in a virtual environment. I am following the beginner tutorial here. The project runs fine, but when I try to run unit tests from the Visual Studio "Test Explorer" they fail with to error: "django.core.exceptions.AppRegistryNotReady: App aren't loaded yet." This is my test class: import datetime from django.test import TestCase from django.utils import timezone from .models import Question class QuestionTestCase(TestCase)

How to test a Django form with a ModelChoiceField using test client and post method

六眼飞鱼酱① 提交于 2019-12-22 18:29:20
问题 How do I use Django test client.post to test a form that has a ModelChoiceField? How should the data dictionary passed to the post method be written? The way I am doing does not select any value at all. I have a form with the following field: country = forms.ModelChoiceField( label="País", queryset=Country.objects.all().order_by('name'), required=True, widget=forms.Select(attrs={ 'onchange': "Dajaxice.party.update_country(Dajax.process, {'option':this.value})" }, ) I also have the following

How to test a Django form with a ModelChoiceField using test client and post method

心已入冬 提交于 2019-12-22 18:29:06
问题 How do I use Django test client.post to test a form that has a ModelChoiceField? How should the data dictionary passed to the post method be written? The way I am doing does not select any value at all. I have a form with the following field: country = forms.ModelChoiceField( label="País", queryset=Country.objects.all().order_by('name'), required=True, widget=forms.Select(attrs={ 'onchange': "Dajaxice.party.update_country(Dajax.process, {'option':this.value})" }, ) I also have the following

Django 1.7 ImageField form validation

前提是你 提交于 2019-12-22 09:32:59
问题 I'm writing unit tests using Django 1.7 with Python 3.4. The form below validates fine when the file_data element is commented out. With the file_data included it doesn't validate and the test fails. from django.core.files.uploadedfile import SimpleUploadedFile ... data = { 'phone_number': '123456', 'job': Job.objects.latest('id').pk, } file_data = { 'portrait': SimpleUploadedFile( content=b'', content_type='image/jpeg', name='test.jpg', ) } form = PersonForm(data, file_data) self.assertTrue

TypeError when concatenating django.test.LiveServerTestCase's live_server_url with another string

三世轮回 提交于 2019-12-20 03:16:48
问题 Whenever I try to construct a string based on self.live_server_url , I get python TypeError messages. For example, I've tried the following string constructions (form 1 & 2 below), but I experience the same TypeError . My desired string is the Live Server URL with "/lists" appended. NOTE: the actual test does succeed to create a server and I can manually access the server, and more specifically, I can manually access the exact URL that I'm trying to build programmatically (e.g. 'http:/

Testing django application with several legacy databases

走远了吗. 提交于 2019-12-19 07:32:16
问题 I have django application with 5 legacy databases. Almost all models are set with the meta attribute managed=False . Since managed=False is set, migrations for each model have been created with the option managed=False . And since, django test runner picks existing migrations for each model to create test tables in test_databases, it simply doesn't create anything. I tried creating test.py settings file with the following workarounds: from web_services.settings.dev import * from django.test

django test client doesnt call POST, but GET

蹲街弑〆低调 提交于 2019-12-13 08:16:04
问题 I try to test view calling by POST. I use follow=True . But test client uses GET method and my POST data are not passed. here is my view: def aaa(request): n = request.method d = request.POST template = 'shop/test.html' return render(request, template, d) Here is my test: from django.utils import unittest from django.test.client import Client def test_add_to_cart_page(self): response = self.client.post('/aaa/', {'product': 11}, follow=True) self.assertEqual(response.status_code, 200) When the

Django test database not auto-flushing

眉间皱痕 提交于 2019-12-12 11:17:54
问题 I have a bunch of unit test files, all of which consist of django.test.TestCase classes. Wrote myself a little shell script to uncomment/comment test file imports in my __init__.py file, so I can run tests from certain test files, based off the command line arguments I give it. I am also able to run all the tests of all the test files in one go (for regression testing purposes). I have this one test file that has some JSON fixtures and the first test checks that a certain model/table has 3

Django: object has no attribute 'was_published_recently'

房东的猫 提交于 2019-12-12 04:17:05
问题 New to Django, I've been doing the 1st tutorial and I'm at part 5 now, which is automated testing. After following the tutorial until step " Fixing the Bug ", it pops up an error when I run the test, as follows: Traceback (most recent call last): File "D:\Python\Django\ui1\polls\tests.py", line 13, in test_was_published_recently_with_future_question self.assertIs(future_question.was_published_recently(), False) AttributeError: 'Question' object has no attribute 'was_published_recently' ------

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