django-testing

Setting liveserver port when running tests in django

荒凉一梦 提交于 2020-01-14 10:32:37
问题 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

How to access request.user while testing?

依然范特西╮ 提交于 2020-01-03 20:06:30
问题 I just moved from Django 1.3.1 to Django 1.4. Right after doing so, a real big number of my tests started to raise these errors: Traceback (most recent call last): File "/Volumes/Data/ADay/Website/Backend/(trunk)/project/tests/templatetags.py", line 406, in setUp self.context = template.RequestContext(self.request) File "/Library/Python/2.6/site-packages/django/template/context.py", line 176, in __init__ self.update(processor(request)) File "/Volumes/Data/ADay/Website/Backend/(trunk)/project

Specifying Readonly access for Django.db connection object

妖精的绣舞 提交于 2020-01-03 17:23:47
问题 I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have such a large amount of data, I really have to test against my production database for the tests to be meaningful. What I'm trying to figure out is how I can define a read-only database connection that is specific to that command or connection object.

Specifying Readonly access for Django.db connection object

橙三吉。 提交于 2020-01-03 17:23:21
问题 I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have such a large amount of data, I really have to test against my production database for the tests to be meaningful. What I'm trying to figure out is how I can define a read-only database connection that is specific to that command or connection object.

Problems using User model in django unit tests

廉价感情. 提交于 2019-12-31 10:32:35
问题 I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase): def setUp(self): self.u1 = User.objects.create(username='user1') self.up1 = UserProfile.objects.create(user=self.u1) def testA(self): ... def testB(self): ... When I run my tests, testA will pass sucessfully but before testB starts, I get the following error: IntegrityError: column username is not unique It's clear that it is trying to create self.u1 before each test case and finding that it

Setting a session variable in django tests

强颜欢笑 提交于 2019-12-30 06:00:19
问题 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 ======================================================================

django test ValueError: the view didn't return an HttpResponse object. It returned None instead

﹥>﹥吖頭↗ 提交于 2019-12-25 06:54:35
问题 I have view to submit data. From the web browser, i can submit data and redirected nicely. But on the test, i got the error: didn't return an HttpResponse object. It returned None instead. How can i pass my test? The view code is: def insert_barang_po(request, po_id=None): "submit barang2 dalam PO, ditamilkan setelah insert PO" po = get_object_or_404(PurchaseOrder, id=po_id) context = {'menu_sales': True,} context['po'] = {'id': po.id,} if request.method == 'POST': form = InsertBarangPOForm

Django MySQL error when running tests in a virtualenv

情到浓时终转凉″ 提交于 2019-12-24 22:44:03
问题 I have a Django app working perfectly on my local Python 3.6 version and want to make sure that it will do so when installed elsewhere. For this reason I created a virtualenv using precisely the same Python version which works fine globally, but without any packages: virtualenv --no-site-packages --python=$(which python3.6) clear_env source clear_env/bin/activate Then I installed the requirements locally: pip install -r requirements.txt When I try to run the server, or even when I used the

Testing 'class Meta' in Django models

╄→гoц情女王★ 提交于 2019-12-24 09:49:05
问题 How can you test ordering , unique and unique_together in Django models? 回答1: Ordering - create a couple of instances, then just check if the order is right. Example: self.assertEqual(list[0], a) self.assertEqual(list[1], b) etc Unique - create an instance, try to create another one with the same unique field. Assert that there is an exception: with self.assertRaises(IntegrityError): MyModel.objects.create(unique_field=the_same_value) Unique Together is the same as unique . Hope it helps! 回答2

Prepopulate database with fixtures or with script?

你说的曾经没有我的故事 提交于 2019-12-24 00:24:45
问题 I'm not an expert but I think is a good idea using a Class to define choices and to prepopulate the database with these choices. I think that make easier to change choices, etc So in my models.py I have: class City(models.Model): name = models.CharField(max_length=32) distance = models.SmallIntegerField(blank=True, null=True) #etc class OtherClass(models.Model): name = models.CharField(max_length=32) #etc class UserProfile(models.Model): name = models.CharField(max_length=32) city = models