I am using pytest-django to test my django application. My directory structure is like this :
.
├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── README.md
├── app
│ ├── __pycache__
│ │ └── test_example.cpython-37-pytest-5.2.1.pyc
│ ├── app
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── settings.cpython-37.pyc
│ │ │ └── urls.cpython-37.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── admin.cpython-37.pyc
│ │ │ └── models.cpython-37.pyc
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ ├── 0001_initial.py
│ │ │ ├── __init__.py
│ │ │ └── __pycache__
│ │ │ ├── 0001_initial.cpython-37.pyc
│ │ │ └── __init__.cpython-37.pyc
│ │ ├── models.py
│ │ └── tests
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── test_admin.cpython-37-pytest-5.2.1.pyc
│ │ │ └── test_models.cpython-37-pytest-5.2.1.pyc
│ │ ├── test_admin.py
│ │ └── test_models.py
│ ├── db.sqlite3
│ ├── manage.py
│ ├── pytest.ini
│ └── test_example.py
└── docker-compose.yml
The file : test_models.py is like below :
from django.contrib.auth import get_user_model
import pytest
#pytest.mark.django_db
class TestUserCreation:
def test_create_user_with_email_successfull(self):
"""
Test creating a new user with an email is successfull
"""
email = "test#gmail.com"
password = "testpass123"
user = get_user_model().objects.create_user(
email=email,
password=password
)
assert user.email == email
assert user.check_password(password)
def test_new_user_email_normalized(self):
"""Test the email for a new user is normalized"""
email = "test#GMAIL.COM"
user = get_user_model().objects.create_user(
email=email,
password="Testpass123"
)
assert user.email == email.lower()
def test_new_user_invalid_email(self):
"""Test creating user with no email raises error"""
with pytest.raises(ValueError):
get_user_model().objects.create_user(
email=None,
password="testpass123"
)
def test_create_new_super_user(self):
"""Test creating a super user"""
user = get_user_model().objects.create_superuser(
email='test#gmail.com',
password='testpass123',
)
assert user.is_superuser
assert user.is_staff
and the file : test_admin.py is like so :
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
def test_something_simple():
names = ["Subhayan", "Shaayan"]
assert len(names) == 2
when i run pytest from the root of my project:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api
plugins: django-3.5.1
collected 6 items
app/core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull SKIPPED [ 16%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized SKIPPED [ 33%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email SKIPPED [ 50%]
app/core/tests/test_models.py::TestUserCreation::test_create_new_super_user SKIPPED [ 66%]
app/test_example.py::test_example PASSED [ 83%]
app/core/tests/test_admin.py::test_something_simple PASSED [100%]
========================================================================== 2 passed, 4 skipped in 0.42s ==========================================================================
But very strangely when i go inside the app folder:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ cd app
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
Django settings: app.settings (from ini file)
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app, inifile: pytest.ini
plugins: django-3.5.1
collected 6 items
core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull PASSED [ 16%]
core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized PASSED [ 33%]
core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email PASSED [ 50%]
core/tests/test_models.py::TestUserCreation::test_create_new_super_user PASSED [ 66%]
test_example.py::test_example PASSED [ 83%]
core/tests/test_admin.py::test_something_simple PASSED [100%]
=============================================================================== 6 passed in 0.81s ================================================================================
I am not sure what is wrong . Can someone please throw some more light on this . The pytest.ini file is as follows
DJANGO_SETTINGS_MODULE = app.settings(recipe-app-api)
Related
I am trying to import my app function which is inside front/app __init__, but somehow from front.app import app it doesnt work from tooler.py.
.
├── settings
│ ├── settings.json
├── front
│ ├── app
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ └── views.cpython-36.pyc
│ │ ├── static
│ │ │ ├── css
│ │ │ │ └── demo.css
│ │ │ ├── data.json
│ │ │ └── js
│ │ │ └── script.js
│ │ ├── templates
│ │ │ └── public
│ │ │ └── index.html
│ │ ├── views.py
│ │ └── views.pyc
│ └── run.py
├── tooler.py
├── __pycache__
│ └── utils.cpython-36.pyc
├── requirements.txt
├── tester.py
├── tree
└── utils.py
run.py
from app import app
if __name__ == "__main__":
app.run()
init
from flask import Flask
app = Flask(__name__)
from app import views
I expect the output of running the server flask, but the actual output is ModuleNotFoundError: No module named 'app'
I need to configure pytest in my django application. But the structure of the directories is different of what I'm used to.
.
├── apps
│ ├── __pycache__
│ ├── admcore
│ ├── ...
│ ├── cauth
│ ├── webservice
│ └── webshell
├── doc
│ └── ...
├── lib
│ └── ...
├── ...
└── project
├── __init__.py
├── globalsettings.py
├── local
│ ├── __init__.py
│ ├── app.uwsgi
│ ├── manage.py
│ ├── settings.py
│ └── wsgi.py
├── mailconf.py
└── urls.py
I created pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE=sgp.local.test_settings
addopts = --nomigrations --cov=. --cov-report=html
and test_settings.py files
#!/usr/bin/env python
import os
import sys
from project import globalsettings
SECRET_KEY = "12341234AFDQFDQSDFAZR123D"
ROOT_PATH = '/usr/local/sgp'
BASEURL_ROOT = ''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
}
And I put the two files in project directory.
.
├── apps
│ ├── __pycache__
│ ├── admcore
│ ├── ...
│ ├── cauth
│ ├── webservice
│ └── webshell
├── doc
│ └── ...
├── lib
│ └── ...
├── ...
└── project
├── __init__.py
├── globalsettings.py
├── local
│ ├── __init__.py
│ ├── app.uwsgi
│ ├── manage.py
│ ├── pytest.ini
│ ├── settings.py
│ ├── test_settings.py
│ └── wsgi.py
├── mailconf.py
└── urls.py
But it doesn't work as I expected. I have one test in webservice app, but when I run pytest command I got:
platform darwin -- Python 3.7.4, pytest-5.2.0, py-1.8.0, pluggy-0.13.0
Django settings: project.local.test_settings (from environment variable)
rootdir: /project/local, inifile: pytest.ini
plugins: django-3.5.1, cov-2.7.1
collected 0 items
---------- coverage: platform darwin, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov
=========================================================================================================== no tests ran in 0.15s ============================================================================================================
I have no ideia how to configure pytest to this project.
My project results are as follows, django in automl/service, in automl/service/worker/suggester.py file has an import from automl.core import util and this file imported by views.py, when I run python service/manage.py runserver will raise the exception
from automl.core import util
ModuleNotFoundError: No module named 'automl'
how to solve this problem, does django can not run in a sub package?
├── automl
│ ├── __init__.py
│ ├── core
│ │ ├── __init__.py
│ │ └── base.py
│ ├── example
│ │ ├── __init__.py
│ │ └── demo.py
│ └── service
│ ├── __init__.py
│ ├── manage.py
│ ├── master
│ │ ├── __init__.py
│ │ └── urls.py
│ ├── settings.py
│ ├── worker
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── exceptions.py
│ │ ├── models.py
│ │ ├── suggester.py
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── urls.py
│ └── wsgi.py
├── bin
├── build.sh
├── ci.yml
├── conf
│ └── log_config.json
└── docs
I am trying to call a function from a python file that is located outside the django project directory from views.py. I have tried importing the python file but running the django server says "no module named xxxx".
Tree structure is given below.
├── auto_flash
│ ├── __init__.py
│ └── test.py
└── fireflash
├── detection_reports
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── __pycache__
│ │ └── __init__.cpython-35.pyc
│ ├── templates
│ │ └── detection_reports
│ │ └── home.html
│ ├── tests.py
│ ├── views.py
│ └── views.pyc
├── fireflash
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ └── settings.cpython-35.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── ui_cgi.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── __init__.py
└── manage.py
Project name here is "fireflash" and there is an app in that project named "detection_reports". There is another directory named "auto_flash" that has same location as "fireflash". What I want to do is to import test.py file from "auto_flash" in detection_reports.views and call a function from views. Importing like this "from auto_flash import test" throws error "no module named auto_flash".
I have tried all of the above mentioned solutions, but the cleaner solution was to append the path for auto_flash to syspath and the issue was resolved. Thanks to all of you for the efforts.
Move auto_flash to fireflash
In detection_reports add from auto_flash import test.py
django-celery-beat running so far ... tasks get queued and worker is executing simple tasks :)
now I would like to have my celery task to do a query towards the django database (to get a list of IP addresses for which I want to check reachability).
can i use the django 'helper code' to do the query or do I need to make a 'raw' connection from celery to sqlite/mysql?
thanks for any help on this
/pat
celery.py
# http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
#from celery import shared_task
from django.conf import settings
#settings.configure()
#from portal import models
#logger = logging.getLogger(__name__)
#from portal.models import location
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sc_custportal.settings')
app = Celery('sc_custportal')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
#app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
queryset = location.objects.all()
# response = os.system("ping -c 1 8.8.8.8")
# if response == 0:
# print("up!")
error:
[2017-05-04 14:55:09,962: ERROR/PoolWorker-1] Task sc_custportal.celery.debug_task[ecb79b33-7246-4e4e-9b4e-3d7f9b0102dc] raised unexpected: NameError("global name 'location' is not defined",)
Traceback (most recent call last):
File "/home/pat/Documents/Development/sc_custportal/env/local/lib/python2.7/site-packages/celery/app/trace.py", line 367, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/pat/Documents/Development/sc_custportal/env/local/lib/python2.7/site-packages/celery/app/trace.py", line 622, in __protected_call__
return self.run(*args, **kwargs)
File "/home/pat/Documents/Development/sc_custportal/sc_custportal/sc_custportal/celery.py", line 32, in debug_task
queryset = location.objects.all()
NameError: global name 'location' is not defined
tree:
sc_custportal
├── celerybeat.pid
├── celerybeat-schedule
├── db.sqlite3
├── manage.py
├── portal
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── 0002_auto_20170503_1212.py
│ │ ├── 0002_auto_20170503_1212.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── templates
│ │ ├── index.html
│ │ ├── inspinia.html
│ │ ├── issue_tracker.html
│ │ ├── login.html
│ │ ├── login.html.sic
│ │ ├── modal_window.html
│ │ ├── notifications.html
│ │ ├── partials
│ │ │ ├── footer.html
│ │ │ ├── head.html
│ │ │ ├── menu.html
│ │ │ └── scripts.html
│ │ ├── profile.html
│ │ ├── table_data_tables.html
│ │ └── toastr_notifications.html
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── rest
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── serializers.py
│ ├── serializers.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
└── sc_custportal
├── celery.py
├── celery.pyc
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── settings.py.orig
├── urls.py
├── urls.pyc
├── wsgi.py
└── wsgi.pyc
Yes, you can do it like
from portal.models import MyModel
#app.task(bind=True)
def debug_task(self):
MyModel.objects.all()
If you're considering accessing objects recently created/updated, please take care that the transaction commited before starting your task. Typically, you could start your task with a transaction.on_commit(your_task_call).
This answer gives you more details.