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
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
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
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'),
}
}
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.
Set the output of the test report on the setting screen of the target project.
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
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.
With the above settings, the test report will be displayed in Jenkins when the build is executed.
Recommended Posts