Wednesday, April 06, 2016

Jenkins: How to execute Python scripts for test automation



Platform: 

  • Windows 7 VM (Oracle VM ViortualBox)

Software: 

  • Python 3.5.1 (32-bit)
  • Python 3.5 pywin32-220
  • Python unittest-xml-reporting 2.1.0 
  • Python Jenkins 0.4.13
  • Java 8 Update73
  • Jenkins 1.642.4 
  • Jenkins Next Builder Number plugin
  • Jenkins Email Extension Plugin

Python scripts structure example:





Python TestSuite_jenkins.py example:


 

Install:

  1. Download and install python 3.5.1 on Windows 7 VM (get python from https://www.python.org/downloads/)
  2. Download and install python3.5 pywin32 on Windows 7 VM (get pywin32 from https://sourceforge.net/projects/pywin32/files/pywin32/)
  3. Download and install unittest-xml-reporting 2.1.0 on Windows 7 WM (get it from  https://pypi.python.org/pypi/unittest-xml-reporting)
  4. Donwload and install Python Jenkins on Windows 7 VM (get info from  https://pypi.python.org/pypi/python-jenkins)
  5. Download and install Java 8 runtime on Windows 7 VM (get Java 8 from http://www.java.com/en/download/windows_xpi.jsp)
  6. download and install Jenkins on Windows 7 VM (get Jenkins from https://jenkins.io/content/thank-you-downloading-windows-installer/#stable)
  7. Open a web browser, load URL: http://localhost:8080. examine the Jenkins main page is loaded.
  8.  Jenkins main page, select Manage Plugins. Download and install "Email Extension Plugin".

Jenkins Setup:

  •  Configure the PATH in Jenkins:
    1. Jenkins main page, select Manage Jenkins to load Manage Jenkins page.
    2. Manage Jenkins page, select Configure System to load configuration page.
    3. Under Global properties, enable Environment variables and add a new key-value pairs as following:
      name: PYTHON_PATH
      value: C:\Python35\;C:\Python35\python.exe;C:\Python35\lib;C:\Python35\DLLs;C:\Python35\lib\site-packages;C:\Python35\lib\site-packages\win32;C:\Python35\lib\site-packages\win32\lib;C:\Python35\lib\site-packages\Pythonwin
  • Add a Python Job to Jenkins:
    1. Jenkins main page, select New Item. Input Item Name, let's say PythonAutoTestSuites. Select Freestyle project.

  • Setup python Job:
    1. enable Advanced Project Options, select Use custom workspace. input Directory string "C:\eclipse_workspace\python\PATS", which is the OS path to the pyton script main folder.
    2. Under Build section, select Add build step to add a new "Execute Windows batch command". Input following string under Command fields:
      SET PATH=%PATH%;%PYTHON_PATH%
      python C:\eclipse_workspace\python\PATS\TestSuite_jenkins.py

    3. Under Post-build Actions, select Add post-build action to add "Publish Junit test result report". Input following string junder Test report XMLs field.
      test_reports\*.xml

How to reset build number:

  1. Rename <project name> to <project name> - Copy.
  2. Create a new job named <project name>, using 'Copy existing job' option, from <project name> - Copy.
  3. Delete job <project name> - Copy.

How to modify build number to display name and description:

  1. the default build number 1, 2, 3 ... is not user friendly. we can customized and make it display a name and description instead of the number.
  2. to make this work, we need using Python Jenkins module and Jenkins Next Builder Number plugin.
  3. modify TestSuite_jenkins.py as following:
  4. the new build will display "Build 0.0.1 Overnig test" instead of a number, and there's description for the new build.

How to setup email notification and including test result summary inside email:

  1.  select job configuration for PythonAutoTestSuite.
  2.  Under Post-build Actions, select add "Editable Email Notification"
  3. input email addresses under Project Recipient List fields. 
  4. select HTML (text/html) as Content Type.
  5. input $(SCRIPT, template="PythonAutoTestSuites-Subject.groovy"} under Default Subject field.
  6. input ${SCRIPT, template="PythonAutoTestSuites-Content.groovy"} under Default Content field.
  7.  Add an "Always" Trigger, select Send to "Recipient List".
  8. Apply changes to current job.
  9. locate jenkins home directory, let's say C:\Program Files (x86)\Jenkins
  10. create a new folder "email-templates" if it is not exist.
  11. add two groovy script files to email-templates folder: PythonAutoTestSuites-Subject.groovy, PythonAutoTestSuites-Content.groovy. the sample groovy scripts are as following





  12. Jenkins configuration, under E-mail Notification, make sure you have SMTP Server setup properly.
  13. each time you generate a build / test, the receipients will receiving an email notification which including the testing build and test summary informaiton like following:


    How to enable web browser interactive test:

  • If jenkins is loaded as a windows service, the tests which interactive with web browser (selenium webdriver) will not load and display web browser.
  • you have to:
    1. Stop / disable jenkins from windows services
    2. Create a windows batch file, let's call it jenkins.bat with following lines
            SET JENKINS_HOME=C:\Program Files (x86)\Jenkins
            java -jar "C:\Program Files (x86)\Jenkins\jenkins.war"
    3. Modify C:\Program Files (x86)\Jenkins folder security property to give full access control to current user.

    4. Execute jenkins.bat batch file to load jenkins.