I've got a couple of Django projects that I work on, and I use Jenkins for continuous integration purposes. I've had that arrangement up and running for a while and it works well.
I'd like to be able to generate automated test coverage reports and have Jenkins handle them as well. It looked to me like django-jenkins was the way to go for that, so I installed it and coverage.
Here's the relevant sections of my settings.py:
# Jenkins integration
INSTALLED_APPS += ('django_jenkins',)
JENKINS_TASKS = (
'django_jenkins.tasks.with_coverage',
'django_jenkins.tasks.run_pylint',
'django_jenkins.tasks.django_tests',
)
PROJECT_APPS = ['myapp']
Now, I can run python manage.py jtest, and it works as expected. However, if I run python manage.py jenkins, it errors:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 76, in load_command_class
return module.Command()
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django_jenkins/management/commands/__init__.py", line 61, in __init__
for module_name in self.get_task_list()]
File "/home/matthew/Projects/blah/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named django_tests
I'm using the standard Django TestCase and LiveServerTestCase classes as the basis of my tests. Any idea where I'm going wrong here? The documentation seems to imply django_tests has been removed, but I can't find any indication as to how you run the Django tests now.
I'm using Django 1.6.2.
Just realised I've been a bit of a numpty. All I needed to do was drop the django_tests line, like this:
# Jenkins integration
INSTALLED_APPS += ('django_jenkins',)
JENKINS_TASKS = (
'django_jenkins.tasks.with_coverage',
'django_jenkins.tasks.run_pylint',
)
PROJECT_APPS = ['myapp']
And django-jenkins will run the tests without having to explicitly request that it does so.
There is a change in the latest version of django_jenkins (0.18.0) so that the django_jenkins.tasks.with_coverage Jenkins Task is also no longer needed.
Instead, you execute the test run as follows:
python manage.py jenkins --enable-coverage
or
python3 manage.py jenkins --enable-coverage
You can find out more on the project's GitHub Repo.
Related
I've a problem with my Django App, this week I added some models and tests but when I'm about to test it with "python manage.py test", I have this error :
TypeError: __init__() got an unexpected keyword argument 'failfast'
I've tried to see if I have some bugs in my models/serializers/tests (syntax error) and even tried to go back to an ancient working commit, but it doesn't solved my issue when testing ...
Also, my server work when I'm using "python manage.py runserver".
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\test\runner.py", line 633, in run_tests
result = self.run_suite(suite)
File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\xmlrunner\extra\djangotestrunner.py", line 57, in run_suite
runner = self.test_runner(**runner_kwargs)
TypeError: __init__() got an unexpected keyword argument 'failfast'
I don't use the failfast argument in the command line so this error makes no sence for me.
I use Django 2.2.2 !
Here a snippet of what my tests looks like :
import pytz
from logging import info
from django.test import TestCase
from django.utils import timezone
from django.test import TransactionTestCase
from apps.myheart.api.api_acquisitions_markers import acquisitions_markers_get
from apps.myheart.models import Implants, Markers, AcquisitionsMarkers, Acquisitions
from datetime import timedelta, datetime
from .test_authentication import authenticate
class ImplantsAPIViewTestCase(TransactionTestCase):
def setUp(self):
self.implant1 = Implants.objects.create(
num=1, implantation_date=datetime(2012, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital1"
)
self.implant2 = Implants.objects.create(
num=2, implantation_date=datetime(2014, 11, 18, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital2"
)
self.implant3 = Implants.objects.create(
num=34, implantation_date=datetime(2016, 10, 18, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital1"
)
self.token = authenticate()
self.nb_imp = len(Implants.objects.all())
def test_implants_get(self):
info("Test /api/implants/\n")
response = self.client.get("/api/implants/", HTTP_AUTHORIZATION=self.token)
info(response.data)
# We need to check that we have all the files presents in DB
self.assertEqual(len(response.data), self.nb_imp)
info("\n")
I had the same issue with a django project that had worked on python 2 but mysteriously stopped working when I switched to python 3.
For me the solution was to switch from the xmlrunner package to unittest-xml-reporting
I.e. try this :-
pip3 uninstall xmlrunner
pip3 install unittest-xml-reporting
The xmlrunner package is no longer supported, unittest-xml-reporting is the replacement.
I just found my bug, seems it was XMLRunner and unittest-xml-reporting that reported me this error. Since I was using them for some times and it worked great I don't know why there are not working anymore but removing them fixed it.
Django Testing: init() got an unexpected keyword argument 'failfast'
Took me awhile to figure this out with the help of this site. Adding answer for pipenv.
pipenv uninstall xmlrunner
pipenv install unittest-xml-reporting
I'm working with Django version 2.2 . When I run the command
python manage.py collectstatic
I get the following log on bash terminal.
You have requested to collect static files at the destination
location as specified in your settings:
/home/djapp/poorface/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
collected = self.collect()
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
handler(path, prefixed_path, storage)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 353, in copy_file
self.storage.save(prefixed_path, source_file)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 49, in save
return self._save(name, content)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 288, in _save
os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)
I changed the permissions of the staticfiles to -rwxrwxrwx . Then also there is no change in output.
manage.py looks like
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Poorface.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
This is a hack - not a proper fix!
I had this problem on both OSX and Ubuntu. Seems that certain packages end up with file permission as string, whereas most are integer.
I hacked django to fix this and identified the package causing the problem - django-filebrowser - probably best not to hack django but if desperate here is the change in core/files/storage.py line 289::
if self.file_permissions_mode is not None:
if type(self.file_permissions_mode) == type("duck"):
print(full_path, self.file_permissions_mode, type(self.file_permissions_mode))
self.file_permissions_mode = int(self.file_permissions_mode)
os.chmod(full_path, self.file_permissions_mode)
This reported::
/home/django/teamup/shared_static/filebrowser/css/filebrowser.css 0644 <class 'str'>
I tried changing the file permissions for filebrowser in place in the virtualenv and rerunning but this did not fix the problem so I've gone with the hack.
Yes this question is old but I run into this now again and found the error-source and the proper fix after some tufts of grey hair, tampering all sorts of of file-access-rights and one simple debug-run of "manage.py collectstatic":
In the olden days or maybe in some different context (can't remember how this crept in to my project-dev-settings - in my production setting it was correct) it is/was fine to set the "FILE_UPLOAD_PERMISSIONS" in the settings.py like this:
FILE_UPLOAD_PERMISSIONS = "0644"
According to the docs at least since Django 1.8 the correct setting is:
FILE_UPLOAD_PERMISSIONS = 0o644
after I knew the reason an the solution I also found a few hints that's "now with python 3 … octals … etc. etc." but no connection to this enervating vague error message:
… os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)
after I used a command python manage.py collectstatic which "allways™" (I know) worked.
So I hope you don't get this much grey hair over some slightly stale projects like me!
So I'm working on this test app in Django and i wanted to add some data to my sql tables through the python shell. I ran the following command from the CMD
λ python manage.py shell
Which gave me the following output:
Traceback (most recent call last): File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv) File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute() File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Python\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options) File "C:\Python\lib\site-packages\django\core\management\commands\shell.py", line 99, in handle
return getattr(self, shell)(options) File "C:\Python\lib\site-packages\django\core\management\commands\shell.py", line 35, in ipython
from IPython import start_ipython File "C:\Python\lib\site-packages\IPython\__init__.py", line 55, in <module>
from .terminal.embed import embed File "C:\Python\lib\site-packages\IPython\terminal\embed.py", line 15, in <module>
from IPython.core.interactiveshell import DummyMod, InteractiveShell File "C:\Python\lib\site-packages\IPython\core\interactiveshell.py", line 109, in <module>
_assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) AttributeError: module 'ast' has no attribute 'AnnAssign'
I'm using Python 3 and Django 2.0.3.
Some things I tried on my own:
I checked that ipython was up to date using pip, my database runs on SQL community server runs and works fine and the Django server itself also runs without issues.
EDIT: I did some more digging AST is part of the standard python library. However i dont find any issues with my python install, any ideas?
EDIT2: Tried reinstalling Ipython, same traceback error.
Alright, so i did a complete reinstall of Django and Mysqlclient through PIP which solved the problem. I now have acess to the django shell.
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.7/site-packages/dj_static.py", line 83, in __call__
return self.application(environ, start_response)
File "/Library/Python/2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 220, in handle_uncaught_exception
if resolver.urlconf_module is None:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/WillRedington/Desktop/Django Projects/propadev/propvocab/urls.py", line 3, in <module>
from rest_framework import routers
File "/Library/Python/2.7/site-packages/rest_framework/routers.py", line 23, in <module>
from rest_framework import views
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 11, in <module>
from rest_framework.compat import HttpResponseBase, View
File "/Library/Python/2.7/site-packages/rest_framework/compat.py", line 13, in <module>
from django.utils.six.moves.urllib import parse as urlparse
ImportError: No module named urllib
The server error message: [17/Dec/2014 16:26:45] "GET / HTTP/1.1" 500 59
This is checking if python returns the modules:
>>> import django.utils.six.moves
>>> import django.utils.six.moves.urllib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named urllib
>>>
My current python version is 2.7.2, Django is 1.5.1, rest framework is in 2.7 site-package.
I have already tried uninstalling and reinstalling Django several times and get the same error.
This is running on Mac OS X 10.9.5, please help me, I've been at this for 5 hours.
it seems like a dependency error. Maybe you downgraded your django, or randomly installed the rest framework package without controlling the dependencies.
django.utils.six.moves.urllib doesnt exist in django==1.5.1 yet. You can try to upgrade django to 1.5.7 for example. Then this import will work. Other things might still be broken though, I can´t tell from here. If you post the output of pip freeze here, it may be easier to help.
You aren´t working in a virtual environment, which is dangerous for dependencies. Best is to make a virtualenv, then install your packages with pip, taking care of dependencies.
Then run:
pip freeze > requirements.txt
and use that file in the future like this:
pip install -r requirements.txt
and edit the file accordingly if you install, update or remove packages.
Something that might be of interest is this: it generally never works to uninstall and reinstall the same package several times; if it doesn´t work once, it won´t work the second time either.
As someone mentioned before my response wasn't really an answer. I solved this error and potentially many more by installing a python virtual environment. To do this run the following
sudo pip install virtualenv
Then create a new folder for your virtual environments. cd into the created folder.
cd myvirtualenv
Then create a new virtual environment by running the following:
virtualenv venv
To run the virtual env:
source venv/bin/activate
This makes a seperate environment with it's own site packages, which is essential for any python developer, especially when working on multiple projects.
Source: http://docs.python-guide.org/en/latest/dev/virtualenvs/
I tried to install django-chronograph to django-1.7 for assigning scheduled task in my django web-app.
I followed the instruction as shown here but it gives me the following error when running python manage.py makemigrations or python manage.py syncdb:
user#(none):~/mysite$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/local/lib/python2.7/dist-packages/chronograph/admin.py", line 2, in <module>
from django.conf.urls.defaults import patterns, url
ImportError: No module named defaults
Is it django-chronograph not supported by django-1.7?
I've never heard of this package, but if you search for that error you will find that that import path hasn't worked since Django 1.6.
It could be that it's just the PyPI version that's old, and that the master branch works fine. However, the last commit to this package was in March 2013, and there's an open issue on the project's bitbucket page indicating that it fails to work on 1.6, so I doubt it.
In sum, it appears that this package supports neither Django 1.6 nor 1.7.
They already committed a fix, so to get rid of this error, don't go through:
pip install django-chronograph
I've just installed on Django 1.8 without issues (so far..) using:
pip install -e hg+https://bitbucket.org/wnielson/django-chronograph#f561106f6aaab62f2817e08e51c799320fd916d9#egg=django-chronograph