django-testing

Testing Django apps that use South migrations

南笙酒味 提交于 2019-12-02 17:07:25
I'm trying to create some Functional tests for a Django app that uses South migrations. Eventually, I will also be creating Twill tests. When I try to run the existing tests, the test database is not created successfully because of a problem with the South migrations. ( Fails @ 7th of 58 migrations ) It would seem that for the purpose of testing, it's better to build the test database from the Django models, like a syncdb would, than from South. How do I set up the Django test suite to build the test database from the current model vs. trying to use South? Leopd The South setting SOUTH_TESTS

how to get request object in django unit testing?

≯℡__Kan透↙ 提交于 2019-12-02 16:07:55
I have a function as def getEvents(eid, request): ...... Now I want to write unit test for the above function separately (without calling the view). So how should I call the above in TestCase . Is it possible to create request ? Mariusz Jamro See this solution : from django.utils import unittest from django.test.client import RequestFactory class SimpleTest(unittest.TestCase): def setUp(self): # Every test needs access to the request factory. self.factory = RequestFactory() def test_details(self): # Create an instance of a GET request. request = self.factory.get('/customer/details') # Test my

Why doesn't unittest.mock.ANY work correctly with Django objects?

ぃ、小莉子 提交于 2019-12-02 15:39:11
问题 I have written a test in Django, and I'm using unittest.mock.ANY to ignore certain values in a dictionary. Here is the test: from django.test import TestCase from django.contrib.auth import get_user_model import unittest.mock as mock class Example(TestCase): def test_example(self): user = get_user_model().objects.create_user(username='example') result = {'user': user, 'number': 42} self.assertEqual( result, {'user': mock.ANY, 'number': 42} ) If I run this test, I expect it to pass. Instead, I

Testing email sending

给你一囗甜甜゛ 提交于 2019-12-02 15:01:19
Any tips on testing email sending? Other than maybe creating a gmail account, especially for receiving those emails? I would like to, maybe, store the emails locally, within a folder as they are sent. You can use a file backend for sending emails which is a very handy solution for development and testing; emails are not sent but stored in a folder you can specify! Davor Lucic 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

Django Rest Framework testing save POST request data

非 Y 不嫁゛ 提交于 2019-12-02 12:05:56
I'm writing some tests for my Django Rest Framework and trying to keep them as simple as possible. Before, I was creating objects using factory boy in order to have saved objects available for GET requests. Why are my POST requests in the tests not creating an actual object in my test database? Everything works fine using the actual API, but I can't get the POST in the tests to save the object to make it available for GET requests. Is there something I'm missing? from rest_framework import status from rest_framework.test import APITestCase # from .factories import InterestFactory class

Wagtail unit testing: Adding child pages converts them to base type

≯℡__Kan透↙ 提交于 2019-12-02 09:40:59
Trying to create some unit tests for Wagtail and running into the following problem: >> root = FrontPage.add_root(instance=FrontPageFactory.build()) >> root <FrontPage: article0> >> root.add_child(instance=ArticlePageFactory.build()) <ArticlePage: article1> >> root.get_tree() <PageQuerySet [<Page: article0>, <Page: article1>]> "article0" goes from being type ArticlePage to type Page in the page tree. Is this Page object a reference to the ArticlePage and there's a method I'm not aware of to fetch it, or am I missing something obvious here? In the meantime I've worked around the problem by just

Why isn't django-nose running the doctests in my models?

穿精又带淫゛_ 提交于 2019-12-02 09:27:05
问题 I'm trying to use doctests with django-nose. All my doctests are running, except not any doctests within a model (unless it is abstract). class TestModel1(models.Model): """ >>> print 'pass' pass """ pass class TestModel2(models.Model): """ >>> print 'pass' pass """ class Meta: abstract = True pass The first doctest does not run and the second does. Why is this? 回答1: Any chance you're getting bitten by this? http://github.com/jbalogh/django-nose/issues/2 If so, try upgrading django-nose 来源:

Why doesn't unittest.mock.ANY work correctly with Django objects?

久未见 提交于 2019-12-02 08:20:15
I have written a test in Django, and I'm using unittest.mock.ANY to ignore certain values in a dictionary . Here is the test: from django.test import TestCase from django.contrib.auth import get_user_model import unittest.mock as mock class Example(TestCase): def test_example(self): user = get_user_model().objects.create_user(username='example') result = {'user': user, 'number': 42} self.assertEqual( result, {'user': mock.ANY, 'number': 42} ) If I run this test, I expect it to pass. Instead, I get this failure: ====================================================================== FAIL: test

Setting a session variable in django tests

余生长醉 提交于 2019-11-30 18:17:53
This works def test_access_to_home_with_location(self): self.client.login(username=self.user.get_username(), password='pass') session = self.client.session session['location'] = [42] session.save() response = self.client.get(reverse('home')) But this def test_access_to_home_with_location(self): session = self.client.session session['location'] = [42] session.save() response = self.client.get(reverse('home')) breaks with ====================================================================== ERROR: test_access_to_home_with_location (posts.tests.HomeViewTestCase) ---------------------------------

Django tests - patch object in all tests

点点圈 提交于 2019-11-30 07:57:06
问题 I need to create some kind of MockMixin for my tests. It should include mocks for everything that calls external sources. For example, each time I save model in admin panel I call some remote URLs. It would be good, to have that mocked and use like that: class ExampleTestCase(MockedTestCase): # tests So each time I save model in admin, for example in functional tests, this mock is applied instead of calling remote URLs. Is that actually possible? I'm able to do that for 1 particular test,