Skip to content

"Database access not allowed" in Doctests (Django) #1180

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

Closed
acjackson opened this issue Nov 12, 2015 · 4 comments
Closed

"Database access not allowed" in Doctests (Django) #1180

acjackson opened this issue Nov 12, 2015 · 4 comments

Comments

@acjackson
Copy link

I'm trying to get database access (django) in a few of my doctests with

pytestmark = pytest.mark.django_db

or

@pytest.mark.django_db

But I keep getting the following error:

Failed: Database access not allowed, use the "django_db" mark to enable it.

Is Django database access supported in doctests?
My other unittests using the Django database work fine.

I use
pytest 1.8.2.dev1 (#1057)
pytest-django==2.9.1
Django==1.8

@nicoddemus
Copy link
Member

IIRC doc tests currently can't be marked with pytest marks, so unless you can enable database access somehow using fixture's and the getfixture API, I'm afraid there's no solution currently.

Perhaps you could create an issue at pytest-dev/pytest-django to see if there are any known workarounds?

@acjackson
Copy link
Author

I tested some more and noticed this: some of the doctests, which are requiring a database connection, run successfully on console, but fail in PyCharm. I could not determine, why some tests run and some don't.

I'm now converting my doctests to unittests, which is probably a better practice and causes less problems.

@nicoddemus
Copy link
Member

That's strange, probably some weird interaction with PyCharm's pytest runner (although I'm a PyCharm user and never experienced any problems). Feel free to reopen this if you happen to have new info.

@pbx
Copy link

pbx commented Dec 31, 2021

Sorry to drop a comment on a six-year-old closed issue, but I'm facing this problem currently and am still looking for a fix.

  • Django 3.2.x
  • pytest 6.2.5
  • pytest-django 4.4.0

My tests were originally inside docstrings in my codebase, but when the recommended decorators didn't help them pass, I extracted them into a standalone .txt file.

Sanitized output showing the error in response to pytest tests.txt:

Traceback (most recent call last):
  File "<doctest tests.txt[2]>", line 1, in <module>
  File "models.py", line 611, in save
    super(Foo, self).save(force_insert=force_insert, using=using)
...
  File "django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "pytest_django/plugin.py", line 714, in _blocking_wrapper
    raise RuntimeError(
RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants