Django test RequestFactory vs Client

RequestFactory and Client have some very different use-cases. To put it in a single sentence: RequestFactory returns a request, while Client returns a response. The RequestFactory does what it says – it’s a factory to create request objects. Nothing more, nothing less. The Client is used to fake a complete request-response cycle. It will create … Read more

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

Django 1.4: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.TestCase.assertRedirects Django 2.0: https://docs.djangoproject.com/en/2.0/topics/testing/tools/#django.test.SimpleTestCase.assertRedirects SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix=”, fetch_redirect_response=True) Asserts that the response returned a status_code redirect status, redirected to expected_url (including any GET data), and that the final page was received with target_status_code. If your request used the follow argument, the expected_url and target_status_code will be the url and status code … Read more

How can I unit test django messages?

I found a really easy approach: response = self.client.post(‘/foo/’) messages = list(response.context[‘messages’]) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), ‘my message’) If you need to check for messages on a response that has no context you can use the following: from django.contrib.messages import get_messages messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), ‘my message’) The fallback storage doesn’t support indexing, however … Read more

Testing email sending in Django [closed]

Django test framework has some built in helpers to aid you with testing e-mail service. Example from docs (short version): from django.core import mail from django.test import TestCase class EmailTest(TestCase): def test_send_email(self): mail.send_mail(‘Subject here’, ‘Here is the message.’, ‘from@example.com’, [‘to@example.com’], fail_silently=False) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, ‘Subject here’)

How should I write tests for Forms in Django?

I think if you just want to test the form, then you should just test the form and not the view where the form is rendered. Example to get an idea: from django.test import TestCase from myapp.forms import MyForm class MyTests(TestCase): def test_forms(self): form_data = {‘something’: ‘something’} form = MyForm(data=form_data) self.assertTrue(form.is_valid()) … # other tests … Read more

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

When Django runs the test suite, it creates a new database, in your case test_finance. The postgres user with username django does not have permission to create a database, hence the error message. When you run migrate or syncdb, Django does not try to create the finance database, so you don’t get any errors. You … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)