-
Notifications
You must be signed in to change notification settings - Fork 24
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-django could not find a Django project #24
Comments
I figured out the following so far:
[pytest]
DJANGO_SETTINGS_MODULE = mysite.settings
python_files = tests.py test_*.py *_tests.py
pythonpath = django vscode ➜ /workspaces/example-docker-django $ pip freeze | grep 'Django\|pytest'
Django==3.2.13
pytest==7.1.2
pytest-django==4.5.2
vscode ➜ /workspaces/example-docker-django $ pytest
================================================================================================================================= test session starts ==================================================================================================================================
platform linux -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
django: settings: mysite.settings (from ini)
rootdir: /workspaces/example-docker-django, configfile: pytest.ini
plugins: django-4.5.2
collected 2 items
django/polls/tests.py F. [100%]
======================================================================================================================================= FAILURES =======================================================================================================================================
_____________________________________________________________________________________________________________________ YourTestClass.test_something_that_will_fail ______________________________________________________________________________________________________________________
self = <tests.YourTestClass testMethod=test_something_that_will_fail>
def test_something_that_will_fail(self):
> self.assertTrue(False)
E AssertionError: False is not true
django/polls/tests.py:16: AssertionError
=============================================================================================================================== short test summary info ================================================================================================================================
FAILED django/polls/tests.py::YourTestClass::test_something_that_will_fail - AssertionError: False is not true
============================================================================================================================= 1 failed, 1 passed in 0.24s ==============================================================================================================================
django.core.exceptions.ImproperlyConfigured: The app module <module 'polls' (namespace)> has multiple filesystem locations (['/tmp/process-executionvZSHuN/django/polls', '/workspaces/example-docker-django/django/polls']); you must configure this app with an AppConfig subclass with a 'path' class attribute. If I add the from django.apps import AppConfig
import os
from django.conf import settings
class PollsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'polls'
path = os.path.join(settings.BASE_DIR, 'polls') Why? I cannot image that this is the right setup. |
To follow up from Slack thread. This is due to there are no inferrable dependency from the polls test to the Adding this dependency explicitly in the BUILD file should resolve this issue: diff --git a/django/polls/BUILD b/django/polls/BUILD
index 0eea8b1..72520e3 100644
--- a/django/polls/BUILD
+++ b/django/polls/BUILD
@@ -2,4 +2,5 @@ python_sources()
python_tests(
name="tests",
+ dependencies=["django/mysite/settings.py"],
) |
That does the trick. As discussed in the Slack thread I tried the new Also,
However, |
Does Django's dev server does some funky re-exec stuff in reload mode that I think is defeated by And in any case, when sandboxed, Django's autoreloading won't work. But (assuming |
Thanks for the explanation, that worked! @kaos any idea for the defaults? I was sure you had a working examples on Slack, but this config does not work as shown above. |
@ptrhck what is not working?
When I comment out the default diff --git a/django/BUILD b/django/BUILD
index e7b4224..f64bed4 100644
--- a/django/BUILD
+++ b/django/BUILD
@@ -6,5 +6,5 @@ pex_binary(
)
__defaults__({
- (python_tests): dict(dependencies=["django/mysite/settings.py"])
- })
\ No newline at end of file
+# (python_tests): dict(dependencies=["django/mysite/settings.py"])
+}) I get just:
Or is it another issue? Edit: just noticed you had another commit in your test repo. Seems you changed from |
Thanks to this example repo, I started experimenting with Django. I have added a simple django project following this tutorial while using the same dependency versions as in this repo. My
pants.toml
looks like this:My project strucuture looks like this
My
pytest.ini
looks as follows:I have been experimenting with the pythonpath option, but I am not able to run the test with
./pants test django/polls/tests.py
as it throws the following error:Any idea? Is the pytest PYTHONPATH messing with the pants PYTHONPATH? I also thought, that I would not need to specify the path as it is a standard Django installation with a default location for
manage.py
.You can find the source code here.
The text was updated successfully, but these errors were encountered: