Configuring Jenkins to run Python tests upon commit

Jenkins can be configured to invoke our test suite upon commit. This is very useful, because we can gear it to track our changes. Teams that use CI systems usually adopt an attitude of addressing CI failures immediately in order to keep the baseline functional.

Jenkins offers an almost unlimited number of features, such as retrieving the latest source from version control, packaging a release, running tests, and even analyzing source code. This recipe shows how to configure Jenkins to run our test suite against our shopping cart application.

Getting ready

  1. Download Jenkins from http://mirrors.jenkins-ci.org/war/latest/jenkins.war.
    Getting ready
  2. Start it up by running java -jar jenkins.war. It's important that no other applications are listening on port 8080.
    Getting ready
  3. Open the console to confirm Jenkins is working.
    Getting ready
  4. Click on Manage Jenkins.
  5. Click on Manage Plugins.
  6. Click on the Available tab.
  7. Find the Git Plugin and click the checkbox next to it.
  8. At the bottom of the page, click on the Install button. Verify that the plugin has successfully installed.
  9. Navigate back to the dashboard screen.
  10. Shutdown Jenkins and start it back up again.
  11. Install git source code control on your machine. You can visit http://git-scm.com/ to find downloadable packages. It is also possible that your system may include package installation options like mac ports or homebrew for Macs, yum for Redhat-based Linux distributions, and apt-get for Debian/Ubuntu systems.
  12. Create an empty folder for this recipe:
    gturnquist$ mkdir /tmp/recipe46
    
  13. Initialize the folder for source code maintenance:
    gturnquist$ git init /tmp/recipe46
    Initialized empty Git repository in /private/tmp/recipe46/.git/
    
  14. Copy the shopping cart application into the folder, add it, and commit the changes.
    gturnquist$ cp cart.py /tmp/recipe46/
    gturnquist$ cd /tmp/recipe46/
    gturnquist$ git add cart.py
    gturnquist$ git commit -m "Added shopping cart application to setup this recipe."
    [master (root-commit) 057d936] Added shopping cart application to setup this recipe.
     1 files changed, 35 insertions(+), 0 deletions(-)
     create mode 100644 cart.py
    

How to do it...

The following steps will show how to put our code under control and then run the test suite when we make any changes and commit them:

  1. Open the Jenkins console.
  2. Click on New Job.
  3. Enter recipe46 as the Job name and pick build a free-style software project.
  4. Click on a.
  5. In the Source Code Management section, pick Git. For URL, enter /tmp/recipe46/.
  6. In the Build Triggers section, pick Poll SCM and enter * * * * * into the schedule box, to trigger a poll once every minute.
  7. In the Build section, select Execute shell and enter the following adhoc script that loads the virtualenv and runs the test suite.
    . /Users/gturnquist/ptc/bin/activate
    nosetests tests.py –with-nosexunit

    You need to substitute the command to activate your own virtualenv, whether this is on Windows, Linux, or Mac, and then follow it with the command used to run the tests just like we did earlier in this chapter.

  8. In the Post-build Actions section, pick Publish JUnit test result report and enter target/NoseXUnit/core/*.xml, so that the test results are collected by Jenkins.
  9. Click on Save to store all the job settings.
  10. Click on Enable Auto Refresh. We should expect the first run to fail, because we haven't added any tests yet.
    How to do it...
  11. Copy the test suite into the controlled source folder, add it, and commit it.
    gturnquist$ cp tests.py /tmp/recipe46/
    gturnquist$ cd /tmp/recipe46/
    gturnquist$ git add tests.py
    gturnquist$ git commit -m "Added tests for the recipe."
    [master 0f6ef56] Added tests for the recipe.
     1 files changed, 20 insertions(+), 0 deletions(-)
     create mode 100644 tests.py
    
  12. Watch to verify whether Jenkins launches a successful test run.
    How to do it...
  13. Navigate to the test results page, where we can see that four of our tests were run.

How it works...

Jenkins provides a powerful, flexible way to configure continuous integration jobs. In this recipe, we configured it to poll our software confirmation management system once a minute. When it detects a change, it pulls a fresh copy of the software and runs our test script.

By using the NoseXUnit plugin, we generated an artifact that was easy to harvest with Jenkins. With a handful of steps, we were able to configure a web page that monitors our source code.

There's more...

Jenkins has lots of options. If you examine the web interface, you can drill into output logs to see what actually happened. It also collects trends showing how long we have had success, when the last build failed, and more.

Do I have to use git for source code management?

The answer is No. We used it in this recipe to quickly show how to install a Jenkins plugin from inside the web interface. To apply the plugin, we had to restart Jenkins.

Subversion and CVS are supported out of the box. Jenkins also has plugins supporting every major source code control system out there, so it should be easy to meet your needs.

In fact, there is support for social coding sites like GitHub and BitKeeper. Instead of using the Git plugin, we could configure our Jenkins installation to watch a certain GitHub account for updates.

What is the format of polling?

We configured the polling with * * * * *, which means once a minute. This is based on the format used to configure crontab files. The columns from left to right are:

  • MINUTE—Minutes within the hour (0-59)
  • HOUR—The hour of the day (0-23)
  • DOM—The day of the month (1-31)
  • MONTH—The month (1-12)
  • DOW—The day of the week (0-7) where 0 and 7 are Sunday

See also

  • Generating a continuous integration report for Jenkins using NoseXUnit
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216