django-testing

Django 1.3: Outbox empty during tests

橙三吉。 提交于 2019-12-05 13:57:27
问题 Maybe I don't understand how outbox works but from the documentation I understood that it just catches all outgoing mail during testing. I created a new project with a new application and added the following code. from django.test import TestCase from django.core.mail import send_mail, outbox class SimpleTest(TestCase): def test_basic_addition(self): send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False) self.assertEqual( len( outbox ),

How should I test a database-driven Django CMS for 404 errors?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 11:53:27
I have designed a basic content management system in Django that uses the database to track Article objects, then displays these Article objects on my website. Each Article uses template tags that I frequently update. Occasionally, I will accidentally break one or more articles on my site when I update a template tag. For example, if I change the required arguments for a template tag referenced by a given article, and forget to update the template tag code within that article, the article will break, resulting in a 404. I would like an easy way to ensure that all of my article pages are still

Django test client response context None

我的未来我决定 提交于 2019-12-05 11:34:27
问题 I have moved my Django app from my development machine (OS X, Python 2.6.5, Django 1.2.3) to a staging server (Ubuntu VM, Python 2.6.6, Django 1.2.3). If I now run my test suite on the staging server, two tests fail when using the Django TestClient because response.context is None (but response.content is correct). For example: self.assertEquals(self.session.pk, response.context['db_session'].pk) These test cases pass on the development machine. Has anybody encountered similar problems? 回答1:

Django manage.py : Is it possible to pass command line argument (for unit testing)

允我心安 提交于 2019-12-05 09:49:25
问题 Is it possible to pass command line arguments to Django's manage.py script, specifically for unit tests? i.e. if I do something like manage.py test myapp -a do_this Can I receive the value do_this in the setUp function of unit test? P.S. @Martin asked the justification for using command line args in tests: Some extensive tests take a lot of time and don't need to be run before every commit. I want to make them optional. Occasional debug messages printed by my test cases should be optional

With py.test, database is not reset after LiveServerTestCase

非 Y 不嫁゛ 提交于 2019-12-05 05:54:43
I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py . This Test Case has uses the LiveServerTestCase and StaticLiveServerTestCase classes (which is a first for me, usually I am using just TestCase ). Adding this new batch of tests in this new file has caused subsequent tests to start failing in py.test (when before they all passed). It appears that the database is not being "reset" after the LiveServerTestCase in py.test. I can tell because of the incrementation of my model's pk values. When I run these tests

How can I keep test data after Django tests complete?

ぃ、小莉子 提交于 2019-12-05 01:00:59
I am using Django 1.8 and the docs say to use --keepdb to save the test database. I am doing that and the database is there but every time I see it, it is empty and has no data in it. Is there any way that I can preserve that so that I can see what's in there? All of your code is running within database transactions, which get rolled back at the end of each test. From the Django testing docs : Here is an example which subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation: This "isolation" means that anything

Models inside tests - Django 1.7 issue

让人想犯罪 __ 提交于 2019-12-04 23:32:02
I'm trying to port my project to use Django 1.7. Everything is fine except 1 thing. Models inside tests folders. Django 1.7 new migrations run migrate command internally. Before syncdb was ran. That means if a model is not included in migrations - it won't be populated to DB (and also to test DB). That's exactly what I'm experiencing right now. What I do is: In my /app/tests/models.py I have dummy model: class TestBaseImage(BaseImage): pass All it does is to inherit from an abstract BaseImage model. Then in tests I create instances of that dummy model to test it. The problem is that it doesn't

Django testing: Test the initial value of a form field

谁说胖子不能爱 提交于 2019-12-04 17:11:28
问题 I have a view that should be setting an initial value for a form field based on a GET value. I want to test this. I'm currently using Django's test client but I am open to looking at other tools. Edit Sorry, I did not mention that I am well aware of the assertContains method but I was hoping there was a better way other than searching the HTML for an input tag and the value attribute. 回答1: Hate to answer my own question (like the 3rd time I've done it) but after mocking around with the test

can't change user permissions during unittest in django

这一生的挚爱 提交于 2019-12-04 09:03:11
I've finally decided to make some tests for my apps but I'm stuck on testing if a user can change another user (depends on the type of the user -- I use django-rules to be able to do logical permission checks, but this is not important) Here's the code I have so far class RulesAndPermissionsTests(TestCase): fixtures = ['auth_no_permissions.json', 'profiles.json', 'rules.json'] def setUp(self): self.c = Client() self.user = User.objects.get(username="estagiario") self.non_staff = User.objects.get(username="fisica") self.admin = User.objects.get(username="admin") login = self.c.login(username=

Running django tests with sqlite

…衆ロ難τιáo~ 提交于 2019-12-04 08:15:29
问题 I use Postgres for production and development, but I'd like to use sqlite to run some tests. I don't see an easy way to configure one engine for tests and another for dev / production. Am I missing something? 回答1: Append the following lines in your settings: import sys if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3' Make sure your actual database setting comes before them. 回答2: This