-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pytest is unable to catch django's IntegrityError #754
Comments
How does the stacktrace for |
I already deleted example project, but here are error logs from another project where I test essentially the same thing: object created with id of related object that doesn't currently exist. IntegrityError logs
But the error is still present.
Expected behaviorIf there is no object A -> IntegrityError is thrown -> deal with it (eg create object A with id=1 and rerun). RealityObject B is created anyway despite having a foreign key constraint, IntegrityError (Key (a_id)=(1) is not present in table "app_a") is thrown but is not fetched by try/except. Everything is messed up. |
have you tried: # tests.py
from django.db import transaction, IntegrityError
import pytest
@pytest.mark.django_db
def test_integrity():
with pytest.raises(IntegrityError):
with transaction.atomic():
B.objects.create(a_id=1) |
https://docs.djangoproject.com/en/2.2/topics/db/transactions/#controlling-transactions-explicitly
I believe you're seeing this because by default @pytest.mark.django_db tests run in an implicit atomic block |
@vanyakosmos I think you need to use |
|
For reference, this can be tested without @pytest.mark.django_db
def test_request_integrity():
from django.db import IntegrityError
from django.db import connection
B.objects.create(a_id=1)
with pytest.raises(IntegrityError) as excinfo:
connection.check_constraints()
assert 'Key (a_id)=(1) is not present in table "app_a"' in str(excinfo.value) Django calls this itself in |
pytest is unable to catch django's IntegrityError
After running
pytest
it will show two errors fortest_integrity
function:Which is basically: "here is an error about not raising IntegrityError in test_foo, and here is an error for raising IntegrityError in test_foo, deal with it"
I can catch Integrity error w/o problems outside of pytest:
pip list
``` Package Version ------------------- --------- apipkg 1.5 asn1crypto 0.24.0 atomicwrites 1.3.0 attrs 19.1.0 certifi 2019.3.9 cffi 1.12.3 chardet 3.0.4 codecov 2.0.15 coverage 4.5.4 cryptography 2.6.1 dj-database-url 0.5.0 Django 2.2.3 django-dbbackup 3.2.0 django-extensions 2.1.7 emoji 0.5.2 execnet 1.6.1 future 0.17.1 gunicorn 19.9.0 idna 2.8 more-itertools 7.0.0 pip 18.1 pluggy 0.11.0 psycopg2-binary 2.8.2 py 1.8.0 pycparser 2.19 pytest 4.5.0 pytest-cov 2.7.1 pytest-django 3.4.8 pytest-forked 1.0.2 pytest-mock 1.10.4 pytest-xdist 1.29.0 python-telegram-bot 12.0.0b1 pytz 2019.1 redis 3.2.1 regex 2019.4.14 requests 2.22.0 schedule 0.6.0 setuptools 40.4.3 six 1.12.0 sqlparse 0.3.0 tornado 6.0.2 urllib3 1.25.3 wcwidth 0.1.7 wheel 0.32.2 whitenoise 4.1.3 ```
Versions:
pip list
from the virtual environment you are usingThe text was updated successfully, but these errors were encountered: