pytest-django | A Django plugin for pytest | Plugin library
kandi X-RAY | pytest-django Summary
kandi X-RAY | pytest-django Summary
A Django plugin for pytest.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Configures the application .
pytest-django Key Features
pytest-django Examples and Code Snippets
building 'psycopg2.\_psycopg' extension
creating build/temp.linux-x86_64-3.9
creating build/temp.linux-x86_64-3.9/psycopg
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-st
py38 inst-nodeps: /home/dafrandle/PycharmProjects/djangoProject/.tox/.tmp/package/1/UNKNOWN-0.0.0.tar.gz
class PeopleFactory(factory.django.DjangoModelFactory):
id = factory.Sequence(lambda x: x + 1)
name = factory.Faker('first_name')
class Meta:
model = People
database = 'my_db2'
pip install coverage
pip install pytest-cov
> pytest --help
...
coverage reporting with distributed testing support:
--cov=[SOURCE] Path or package name to measure during execu
max_queries = django_assert_max_num_queries
@pytest.fixture
def max_queries(django_assert_max_num_queries):
yield django_assert_max_num_queries
import pytest
from pytest_django.plugin import _blocking_manager
from django.db.backends.base.base import BaseDatabaseWrapper
@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
_blocking_ma
from django.db.models import OuterRef, Subquery
toolbox_subquery = Toolbox.objects.filter(tools=OuterRef('pk')).order_by('-version')
tools_qs = Tool.objects.order_by(Subquery(toolbox_subquery.values('name')[:1]))
def pytest_sessionstart(session):
from django.test import TestCase
TestCase.multi_db = True
TestCase.databases = '__all__' # here
# lib.py
from datetime import datetime
from elasticsearch import Elasticsearch
def f():
es = Elasticsearch()
es.indices.create(index='my-index', ignore=400)
return es.index(
index="my-index",
id=42,
b
Community Discussions
Trending Discussions on pytest-django
QUESTION
I have a Django project that i'm trying to set up unit testing with tox in.
here is the directory structure:
ANSWER
Answered 2022-Apr-01 at 05:48The problem is well described in the error log:
QUESTION
I am developing a Django app (Django v3.2.10, pytest v7.0.1, pytest-django v4.5.2) which uses cursor to perform raw queries to my secondary DB: my_db2, but when running tests, all the queries return empty results, like if they were running on parallel transactions.
My test file:
...ANSWER
Answered 2022-Feb-24 at 05:47@hoefling and @Arkadiusz Łukasiewicz were right, I just needed to add the corresponding DB within the factories:
QUESTION
I'm trying to use pytest for the 1st time in my new created app but i'm facing problems with running it. I still receive 'no tests ran in...' message.
My project name is PROJECT, then is my app called diet_app
and file tests.py
inside the app folder.
How to run tests?
I tried with:
pytest
pytest tests
pytest diet_app
To be more precise - to install PyTest I used:
...ANSWER
Answered 2022-Feb-21 at 15:38pytest-django doesn't actually find tests in a file called tests.py
out of the box. If you want to keep using a file called tests.py
you'll need to add a pytest.ini
file in your top-level directory:
My tests are not being found. Why?
By default, pytest looks for tests in files named
test_*.py
(note that this is not the same astest*.py
) and*_test.py
. If you have your tests in files with other names, they will not be collected. Note that Django’s startapp manage command creates anapp_dir/tests.py
file. Also, it is common to put tests underapp_dir/tests/views.py
, etc.To find those tests, create a
pytest.ini
file in your project root and add an appropriatepython_files
line to it:
QUESTION
I have an input file
...ANSWER
Answered 2022-Jan-14 at 10:30Using sed
:
QUESTION
I have a few pytest fixtures I use from third-party libraries and sometimes their names are overly long and cumbersome. Is there a way to create a short alias for them?
For example: the django_assert_max_num_queries
fixture from pytest-django. I would like to call this max_queries
in my tests.
ANSWER
Answered 2022-Jan-10 at 19:56You cannot just add an alias in the form of
QUESTION
I'm attempting to run some unit tests on a StructBlock, which is composed of normal block fields and a StreamBlock.
The problem I'm running into is that I can construct a test that renders the block, but I cannot test the StreamBlock validation (i.e., I can't test the block's clean()
)
Stack
- Python 3.9.6
- Django 3.2
- Wagtail 2.13
- pytest 6.2
- pytest-django 4.4
Block Definitions
MyStructBlock
...ANSWER
Answered 2021-Dec-29 at 15:02Every block type has a corresponding 'native' data type for the data it expects to work with - for the simpler blocks, this data type is what you'd expect (e.g. a string for CharBlock, an Image instance for ImageChooserBlock) but for a few of the more complex ones, there's a custom type defined:
- for RichTextBlock, the native type is
wagtail.core.rich_text.RichText
(which behaves similarly to a string, but also has asource
property where e.g. page IDs in page links are kept intact) - for StreamBlock, the native type is
wagtail.core.blocks.StreamValue
(a sequence type, where each item is a StreamValue withblock_type
andvalue
properties).
The render
method will generally be quite forgiving if you use the wrong types (such as a string for RichTextBlock or a list of dicts for StreamBlock), since it's really just invoking your own template code. The clean
method will be more picky, since it's running Python logic specific to each block.
Unfortunately the correct types to use for each block aren't really formally documented, and some of them are quite fiddly to construct (e.g. a StructValue needs to be passed a reference to the corresponding StructBlock) - outside of test code, there isn't much need to create these objects from scratch, because the data will usually be coming from some outside source instead (e.g. a form submission or the database), and each block will be responsible for converting that to its native type.
With that in mind, I'd recommend that you construct your data by piggybacking on the to_python
method, which converts the JSON representation as stored in the database (consisting of just simple Python data types - integers, strings, lists, dicts) into the native data types:
QUESTION
I am using django for my backend and pytest to handle my tests.
I switched my project db from sqlite3 to postgres, everything works just fine except for the tests, for some reason all of my them are failing.
before switching I was able to access my db during my tests with the following line:
...ANSWER
Answered 2021-Nov-18 at 18:35so adding this code to conftest.py resolved the problem for me
QUESTION
As I develop the backend side, I also develop the frontend. As you have probably guessed, I have to do some REST requests on my server.
Well, there are a couple of solutions for that. I can use mocks on the frontend, or a fake JSON server. However, there are some cons to this approach:
- I have to remodel what I have on an already existing backend server.
- Dealing with authentication on a fake JSON server is not really comfortable.
- I have to write a lot of boilerplate if I want to go on mocking and unit testing approach.
Again, all the logic I want already exists in my Django project, so what I want is:
- Run a dev server.
- Set up a fresh new database.
- Provide some initial data. Some fake users, posts, whatever.
- Do my testing on my frontend project while the server runs.
- Hit the kill signal. (CTRL+C)
- Gracefully drop the database that I have created on step 2.
- Shutdown the program.
I use pytest
and pytest-django
. They do exactly these when they set up a test environment, I was wondering if I can manually do that.
Thanks in advance.
Environment- Python 3.9
- Django 2.2
ANSWER
Answered 2021-Nov-16 at 11:12I never used such integration-testing setup, but I’ve seen it somewhere.
You can run the django-admin testserver mydata.json
command to spin up a testing server with data from a provided fixture. The way it was done in the example I saw was by making a custom Django command where instead of static fixture the testserver’s database was populated dynamically by factories. I don’t remember the details how it was implemented, though.
After the testserver was ready, the Cypress tests were run. Backend and the frontend were wired by npm’s start-server-and-test
package.
Application with similar setup can be checked out here. Especially note the custom testserver command as well as the cypress commands.
QUESTION
Let's say I have the following Django models:
...ANSWER
Answered 2021-Sep-09 at 07:49QUESTION
I am using Django to develop an ERP and I want to use pre-commit with my project.
I have installed pre-commit, black, flake8, flake8-black. and this is my
...ANSWER
Answered 2021-Jun-27 at 07:04This is a known issue with cpython on windows. The error occurs when black tries to run multiple workers on >60 core machines because the default number of process workers given by os.cpu_count()
breaks some other windows limit (number of waiting processes? I'm not quite sure). Black >=19.10b0
has a fix for this, so try updating the version of black in your pre-commit config if you can?
- Python bug report: https://bugs.python.org/issue26903
- Fix applied by this PR in black: https://github.com/psf/black/pull/838
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install pytest-django
You can use pytest-django like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page