Questions tagged [django-testing]

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. This tag is about writing and running tests for your Django apps.

Django

Testing overview

Testing a Web application is a complex task, because a Web application is made of several layers of logic – from HTTP-level request handling, to form validation and processing, to template rendering. With Django’s test-execution framework and assorted utilities, you can simulate requests, insert test data, inspect your application’s output and generally verify your code is doing what it should be doing.

1220 questions
15
votes
1 answer

How to test a Django model with pytest?

I am getting started with pytest. I have configured pytest, anyway I couldn't found a resource on Django specific testing with pytest. How do I test a model with pytest_django? I have already asked a question on unittesting, how do I efficiently…
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
15
votes
2 answers

Models inside tests - Django 1.7 issue

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 -…
tunarob
  • 2,768
  • 4
  • 31
  • 48
15
votes
1 answer

Django test client does not log in

I am attempting to log in the test client using its built in login function. I am trying to unit test views and need to log in to test some of them. I have been trying to do this for too long and need help. A few notes: create_user() does create a…
mdw7326
  • 434
  • 7
  • 28
14
votes
1 answer

How to add authentication token in header of `APIClient` in `django rest_framework test`

I am using oauth2_provider for my rest_framework. I am trying to write test case for my api. I have obtained an access token. But I am not able to authenticate user using access token in APIClient I am looking to get this curl command work with…
14
votes
3 answers

What Django TEST_RUNNER supports xunit xml and logging capture?

I'm attempting to set up a new django project, and I've configured TEST_RUNNER in settings.py to be django_nose.NoseTestSuiteRunner. I chose this test runner because it seems to be the only one I can find that has the following features: writes…
robru
  • 2,313
  • 2
  • 29
  • 38
14
votes
1 answer

Django and postgresql testing schema

I'm using PostgreSQL 9.3 and Django 1.7.4 with psycopg2 2.5.4 The DBA, asked us to create a schema for our application instead of using public. We defined the schema, and we had to add the 'OPTIONS': { 'options': '-c…
14
votes
3 answers

Mock Stripe Methods in Python for testing

So I am trying to mock all the stripe web hooks in the method so that I can write the Unit test for it. I am using the mock library for mocking the stripe methods. Here is the method I am trying to mock: class AddCardView(APIView): """ * Add card…
Shubham
  • 3,071
  • 3
  • 29
  • 46
14
votes
1 answer

Using coverage, how do I test this line?

I have a simple test: class ModelTests(TestCase): def test_method(self): instance = Activity(title="Test") self.assertEqual(instance.get_approved_member_count(), 0) My problem is that coverage still shows…
Prometheus
  • 32,405
  • 54
  • 166
  • 302
14
votes
4 answers

Setting a session variable in django tests

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 =…
Ben
  • 6,986
  • 6
  • 44
  • 71
14
votes
1 answer

Django- Factory boy failing for no apparent reason in just one factory

The code: class StockFactory(UniqueObjectsFactory): FACTORY_FOR = Stock FACTORY_DJANGO_GET_OR_CREATE = ('name', 'market') market = factory.SubFactory(MarketFactory) symbol = FuzzyAttribute(lambda:…
alonisser
  • 11,542
  • 21
  • 85
  • 139
13
votes
6 answers

Generate in-memory image for Django testing

Is it possible to generate an in-memory image for testing purposes? Here is my current code: def test_issue_add_post(self): url = reverse('issues_issue_add') image = 'cover.jpg' data = { 'title': 'Flying Cars', …
Jason Christa
  • 12,150
  • 14
  • 58
  • 85
13
votes
1 answer

Django test fixtures and content types

I'm using django's ContentType foreign key in my model, and I'm using it in the fixtures for unit tests. Therefore, I have to hard-code content_type_id in my fixture, but django sometimes initializes it to a different value and thus my tests…
dragoon
  • 5,601
  • 5
  • 37
  • 55
13
votes
2 answers

Setup postgres in Github Actions for Django

I'm currently working on a website right now on Django. On my computer, I am running it on Docker with a postgres database. Here's the docker-compose file I have: version: '3' services: db: image: postgres environment: …
13
votes
4 answers

Django test Client submitting a form with a POST request

How can I submit a POST request with Django test Client, such that I include form data in it? In particular, I would like to have something like (inspired by How should I write tests for Forms in Django?): from django.tests import TestCase class…
mmagician
  • 1,970
  • 2
  • 15
  • 26
13
votes
2 answers

How to prepopulate test database by necessary data?

I need to do some unit tests in my Django project. The problem is that almost every use case depends on prepopulated database objects. For example, I want to create a product and test, if there were all pre_save signals successful. from…
Milano
  • 18,048
  • 37
  • 153
  • 353