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.

254 questions
30
votes
4 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
  • 1,699
  • 13
  • 19
25
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
  • 13,254
  • 4
  • 42
  • 60
19
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
  • 5,158
  • 8
  • 38
  • 79
17
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,599
  • 2
  • 14
  • 22
16
votes
2 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,173
  • 3
  • 17
  • 19
13
votes
1 answer

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
  • 6,544
  • 10
  • 41
  • 64
13
votes
2 answers

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
  • 18,901
  • 11
  • 65
  • 90
12
votes
1 answer

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

My problem is the one bellow. If I try to run the test, it says there is no database permission and that I have to add that fixture. The problem is that I have added that fixture to any possible method I have, and still nothing. So I assume I don't…
Ev.
  • 1,251
  • 1
  • 16
  • 43
11
votes
3 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
  • 9,116
  • 6
  • 56
  • 78
10
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
  • 1,968
  • 1
  • 19
  • 50
10
votes
5 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
  • 34,968
  • 53
  • 191
  • 361
10
votes
3 answers

How to assert django uses certain 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
  • 4,050
  • 4
  • 28
  • 61
9
votes
2 answers

Django connections object does not see the tables of a second database during testing with pytest-django

Bottom Line: My Django connections object does not see the table relations of a second database during testing with pytest-django. Overview: I have a problem where my Django connections object seems to get the wrong database information. I stumbled…
Scott Skiles
  • 2,933
  • 2
  • 26
  • 50
8
votes
1 answer

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
  • 85
  • 1
  • 3
8
votes
2 answers

How to test file upload in Django rest framework using PUT?

I would like to write a unit test for a view on a Django REST Framework application. The test should upload a file using PUT, in essence equivalent to http -a malkarouri PUT http://localhost:8000/data-packages/upload/ka @tmp/hello.py The code I…
Muhammad Alkarouri
  • 21,347
  • 16
  • 59
  • 94
1
2 3
16 17