Questions tagged [pytest-django]

A plugin for pytest that simplifies testing of django projects.

Pytest-django is a plugin for pytest that helps with the testing of django projects. The package includes specialized fixtures for setting up/tearing down the development server and test client.

387 questions
67
votes
5 answers

Disable autouse fixtures on specific pytest marks

Is it possible to prevent the execution of "function scoped" fixtures with autouse=True on specific marks only? I have the following fixture set to autouse so that all outgoing requests are automatically mocked out: @pytest.fixture(autouse=True) def…
Mattew Whitt
  • 2,194
  • 1
  • 15
  • 19
38
votes
1 answer

How to show warnings in py.test

I just ran py.test on my code and got the following output: ================== 6 passed, 2 pytest-warnings in 40.79 seconds ======================= However, I cannot see what py.test would like to warn me about. How can I turn on warning output to…
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
36
votes
1 answer

Django test VS pytest

I am new to django unittest and pytest. However, I started to feel that pytest test case is more compact and clearer. Here is my test cases: class OrderEndpointTest(TestCase): def setUp(self): user =…
joe
  • 8,383
  • 13
  • 61
  • 109
33
votes
1 answer

pytest ScopeMismatch error: how to use fixtures properly

For the following piece of code: @pytest.fixture(scope="module") def dummy_article(request, db): return mixer.blend("core.article", title="this one price", internal_id=3000) def test_article_str_method(dummy_article): assert ( …
zerohedge
  • 3,185
  • 4
  • 28
  • 63
31
votes
4 answers

pytest and Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it

During invoking pytest from the shell I get the following output, because my test is stored in apps.business.metrics.tools.tests.py, and during import of the module apps/business/metrics/widgets/employees/utilization.py makes a live call to SQL…
DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121
28
votes
1 answer

pytest assert message customization with variable introspection

In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code: def test_api_call(self, client): …
Marc Tudurí
  • 1,869
  • 3
  • 18
  • 22
19
votes
11 answers

VS Code not finding pytest tests

I have PyTest setup in vs-code but none of the tests are being found even though running pytest from the command line works fine. (I'm developing a Django app on Win10 using MiniConda and a Python 3.6.6 virtual env. VS Code is fully updated and I…
Ben
  • 10,931
  • 9
  • 38
  • 47
19
votes
3 answers

Why does pytest-xdist make my tests run slower, not faster?

I'm porting a ~2000 method test suite from nose to pytest because django-nose didn't support parallelization well. Swapping out nose for pytest seemed to work pretty well, and after adding python_files to pytest.ini it found almost all of our…
Cassie Meharry
  • 2,633
  • 3
  • 20
  • 20
17
votes
8 answers

"Apps aren't loaded yet" when trying to run pytest-django

Using the (partial) polls app from the Django tutorial as an example, I'm trying to get pytest-django to run. Using the command django-admin startproject mysite2, I've created a project directory with the following structure: . ├── db.sqlite3 ├──…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
17
votes
3 answers

How to assert django uses particular template in pytest

In the unittest style, I can test that a page uses a particular template by calling assertTemplateUsed. This is useful, for example, when Django inserts values through a template, so that I can't just test string equality. How should I write the…
Hatshepsut
  • 5,962
  • 8
  • 44
  • 80
15
votes
4 answers

PATCH and PUT don't work as expected when pytest is interacting with REST framework

I am building an API using the django REST framework. To test this API I am using pytest and the test client like so: def test_doesnt_find(self, client): resp = client.post(self.url, data={'name': '123'}) assert resp.status_code ==…
David Schumann
  • 13,380
  • 9
  • 75
  • 96
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
14
votes
3 answers

How to use pytest fixtures with django TestCase

How can I use a pytest fixture within a TestCase method? Several answers to similar questions seem to imply that my example should work: import pytest from django.test import TestCase from myapp.models import Category pytestmark =…
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
12
votes
5 answers

How to solve PytestConfigWarning: Unknown config option: DJANGO_ SETTINGS_MODULE error?

I am using django to build my website and I have used django-pytest to test my apps but I have got this error Note I am usign python 3.9 ================================================================== warnings summary…
Dhia Shalabi
  • 1,332
  • 1
  • 13
  • 29
12
votes
2 answers

Installed pytest but running `pytest` in bash returns `not found`

I am following the python-django tutorial in Vagrant (Ubuntu 18.04 / Python3.6.6). After running pip3 install pytest-django and configuring pytest.ini file, running pytest returns Command 'pytest' not found, but can be installed with: apt install…
Hank McDonnel
  • 125
  • 1
  • 1
  • 4
1
2 3
25 26