django-testing

Django: is there a way to count SQL queries from an unit test?

痞子三分冷 提交于 2020-07-16 16:21:03
问题 I am trying to find out the number of queries executed by a utility function. I have written a unit test for this function and the function is working well. What I would like to do is track the number of SQL queries executed by the function so that I can see if there is any improvement after some refactoring. def do_something_in_the_database(): # Does something in the database # return result class DoSomethingTests(django.test.TestCase): def test_function_returns_correct_values(self): self

Want to disable signals in Django testing

戏子无情 提交于 2020-05-09 19:02:07
问题 So I have various signals and handlers which are sent across apps. However, when I perform tests / go into 'testing mode', I want these handlers to be disabled. Is there a Django-specific way of disabling signals/handlers when in testing mode? I can think of a very simple way (of including the handlers within an if TESTING clause) but I was wondering if there was a better way built into Django?... 回答1: No, there is not. You can easily make a conditional connection though: import sys if not

Assertion error while testing Django views

风流意气都作罢 提交于 2020-04-07 07:17:37
问题 This is my testing function for views.py which I have mention below: def test_operation_page(self): url = reverse('operation') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'abc.html') self.assertContains(response, '<b>BOOK id having certain title:</b>') This is the error I am having while testing my views AssertionError: Database queries to 'default' are not allowed in SimpleTestCase subclasses. Either subclass TestCase or

Django test client gets 301 redirection when accessing url

空扰寡人 提交于 2020-04-07 02:38:09
问题 I am writing unittests for django views. I have observed that one of my views returns redirection code 301, which is not expected. Here is my views.py mentioned earlier. def index(request): return render(request, 'index.html', {'form': QueryForm()}) def query(request): if request.is_ajax(): form = QueryForm(request.POST) return HttpResponse('valid') Below is urls.py. urlpatterns = patterns('', url(r'^$', 'core.views.index'), url(r'^query/$', 'core.views.query') ) And unittest that will fail.

Django unit testing: AttributeError: 'WSGIRequest' object has no attribute 'user'

随声附和 提交于 2020-02-05 03:31:30
问题 When running the tests i get outputted following error: user = self.request.user AttributeError: 'WSGIRequest' object has no attribute 'user' I have tried switching from MIDDLEWARE to MIDDLEWARE_CLASSES and vice versa. Currently, I'm running Django 2.1. Also, I have tried switching middleware positions and it didn't help. settings.py MIDDLEWARE = ( "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware",

when does the database is being destroy in django tests?

安稳与你 提交于 2020-01-25 01:06:10
问题 i'm writing tests for my django app, and i can't understand when does the database is destroyed.. in django website it says its being destroy "he test databases are destroyed when all the tests have been executed." i understand from that, that the db is destroyed when the last test command in the last test*.py file is executed. so i wrote some tests and i have the following: UserMethodsTests(TestCase): def test_get_full_name(self): create db objects in model X do some stuff on them (not

django test non-field validation errors

余生颓废 提交于 2020-01-23 00:34:12
问题 What is the django way to test non field specific validation errors? For example, an email field will test is the text entered is a valid email address, but a non-field error will display above the form, with no relationship to any one field. It would, for instance, say that that email address is already registered. When testing applications, how do you test these non-field validation errors? Edit: To make the question more clear. I have a custom validation function, but I'd like to test that

How to launch tests for django reusable app?

廉价感情. 提交于 2020-01-20 13:31:33
问题 Can I launch tests for my reusable Django app without incorporating this app into a project? My app uses some models, so it is necessary to provide (TEST_)DATABASE_* settings. Where should I store them and how should I launch tests? For a Django project, I can run tests with manage.py test ; when I use django-admin.py test with my standalone app, I get: Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. What are the best practises here? 回答1:

How to launch tests for django reusable app?

心已入冬 提交于 2020-01-20 13:30:54
问题 Can I launch tests for my reusable Django app without incorporating this app into a project? My app uses some models, so it is necessary to provide (TEST_)DATABASE_* settings. Where should I store them and how should I launch tests? For a Django project, I can run tests with manage.py test ; when I use django-admin.py test with my standalone app, I get: Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. What are the best practises here? 回答1:

Setting liveserver port when running tests in django

强颜欢笑 提交于 2020-01-14 10:32:48
问题 I am using django for a webapp and I'm using docker to deploy it. I need to test it in the container with selenium. I'm using selenium grid for testing. In order to connect with the liveserver on the docker, i need to port forward a specific port, but as far as i read in the django docs, LiveServerTestCase uses port 0, which means random port every time i run the tests. Since --liveserver options is deprecated, is there any other way to set the port of the test server or a smarter way to test