⬆️ ⬇️

HowTo: continuous integration of Django to Jenkins using Selenium

This is a cheat sheet revealing the "Integration of Selenium tests" section of the article Configuring Jenkins for a django project from scratch . Namely, how to run Selenium tests on a remote Jenkins server that does not have a monitor and air vents.



Thanks to black_bunny for the article and it perfectly covers the installation and Jenkins setup, but I broke a pair of crutches while I combined the whole thing into a working system with his django-selenium . Now short and to the point.



For reference: I have a test server with Ubuntu 11.10 x64 without a monitor and X.org and the described action was performed on it.



Installation


We install and customize Jenkins as described in the above article.

')

To run tests, Selenium will need a browser and an output terminal for this browser itself.



We put him a terminal:

 sudo apt-get install xvfb 


and the browser itself:

 sudo apt-get install --no-install-recommends firefox 


For the tests also need a couple of python packages. Here you can see how to automate the assembly of the test environment . Actually, I use the same fab script with a modified startup command. I will show it later.



Packages for running Selenium tests (installed via pip install ):

 selenium django-selenium 




Customization


The author of django-selenium recommends using my runner of tests for running under Jenkins , but since it is described, it did not work, and when I corrected it and it worked, I did not take into account the PROJECT_APPS setting for django-jenkins , i.e. It launched absolutely all tests from all packages used in the project. Therefore, I offer you my runner, which is launched by django-jenkins , runs tests that are supposed for it, after which it runs exclusively selenium tests from django-selenium :



 # -*- coding: utf-8 -*- from django_jenkins.runner import CITestSuiteRunner from django_selenium.jenkins_runner import JenkinsTestRunner, SeleniumTestRunner from django.test.testcases import TestCase from django.test.simple import reorder_suite class ProjectRunner(JenkinsTestRunner): """ Project test runner """ def __init__(self, **kwargs): super(ProjectRunner, self).__init__(**kwargs) self.selenium_only = True def build_suite(self, test_labels, **kwargs): suite = SeleniumTestRunner.build_suite(self, test_labels, **kwargs) suite.addTest(CITestSuiteRunner.build_suite(self, test_labels, **kwargs)) return reorder_suite(suite, (TestCase,)) 


Put this code in the test_runner.py file in the root of your project (say the project is called “project” ).



Add to project settings:

 SELENIUM_DISPLAY = ':99' JENKINS_TEST_RUNNER = 'project.test_runner.ProjectRunner' 


Launch


In this place:



image



The Jenkins project setting should be slightly edited and replaced by the launch command with:

 xvfb-run ./manage.py jenkins 


In general, you need to put the test execution in the X.org virtual framebuffer, i.e. Before starting the command for running tests, add xvfb-run . At the end you can add a list of applications, although it does not make much sense, because we have a working PROJECT_APPS setting.



That's all - successful tests!

Source: https://habr.com/ru/post/138784/



All Articles