[PYTHON] Until you CI what you made with Django with Jenkins

A memo to write a test in Django, output a test report / coverage report, and output the test result to Jenkins I referred to the following article. Testing with Django

Premise

Bitbucket and Jenkins have been linked. When you push from the local environment to Bitbacket, the Jenkins build will run automatically and the test will be executed. Please refer to the following blogs for building these environments.

About the cooperation between Bitbucket's private repository and Jenkins

Build a test environment with Django

Module installation

Install django-nose and coverage with pip. I used to create a similar environment using unittest-xml-reporting, but I feel that django-nose is a lot easier.

$ pip install django-nose coverage

Settings in the test settings file

Describe the test settings in the test settings file.

# settings_test.py

# -*- coding:utf-8 -*-
from mysite.settings import *
import os

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
#Coverage report(html)Specify the folder to output
COVERAGE_REPORT_HTML_OUTPUT_DIR = '.cover'

#Argument settings when running a test(nose argument)
NOSE_ARGS = [
    '--with-xunit',
    '--with-coverage',
    '--cover-xml',
    '--cover-html',
    '--cover-package=app', #Specify the app name to test
]

#B environment settings for testing(settings.Separated from py's DB environment)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'test.db'),
    }
}

Test run

Create the test code under app (app / tests.py), specify settings_test in the configuration file as shown below, and execute the test.

$ python manage.py test app --settings=mysite.settings_test

Success if the coverage report (html) is output to mysite / cover and nosetests.xml and coverage.xml are output directly under mysite. Now you are ready to CI with Jenkins.

View test report in Jenkins

Set the output of the test report on the setting screen of the target project.

Write a command to run the test

Select Run Shell for Build and write a command to run the following test in the shell script text area. When the Jenkins build is executed, the described command is executed.

#Run virtualenv(Build an environment in any location in advance)
source /var/www/django/mysite/venv/bin/activate
#Run the test
python manage.py test app --settings=mysite.settings_test

Specifying the test report (xml file)

Enter the output xml file name for the Cobertura XML report pattern of post-build processing and the aggregation of JUnit results. This time, xml is output directly under mysite, so you can enter the file name as it is. スクリーンショット 2014-11-11 1.15.54.png

With the above settings, the test report will be displayed in Jenkins when the build is executed.

スクリーンショット 2014-11-11 1.29.31.png

Recommended Posts

Until you CI what you made with Django with Jenkins
Until you start Jupyter with Docker
Twitter posting application made with Django
Until you run python with apache
Until you shake ruby with renpy
Python | What you can do with Python
Your own Twitter client made with Django
A simple RSS reader made with Django
Note until you use emacs with WSL
What you can do with API vol.1
Until you confirm iOS communication with mitmproxy
I made CORS custom middleware with Django
Until you publish (deploy) a web application made with bottle on Heroku
What you can do with programming skills
Until you can use opencv with python
I made a WEB application with Django
Until you use the Kaggle API with Colab
What are you comparing with Python is and ==?
What Python beginners got hooked on with Django
Until you use PhantomJS with Python on Heroku
Until you create a new app in Django
What are you using when testing with Python?
What is Django? .. ..
Internationalization with django
CRUD with Django
Links to do what you want with Sublime Text
When you want to filter with Django REST framework
Web application made with Python3.4 + Django (Part.1 Environment construction)
pickle To read what was made in 2 series with 3 series
Until you run server Django in Visual Studio Code
Deploy a Django app made with PTVS on Azure
[GitHub] Until you pull with ssh using Deploy keys
What you can't do with hstack or vstack with dstack
Things to do when you start developing with Django
Until you install TensorFlow-GPU with pip in Windows environment
What you can and cannot do with Tensorflow 2.x
Until you can do simple image recognition with Jupyter