Jenkins + Docker + Python

Jenkins + Docker + Python

This is going to be a very brief tutorialish article, nothing too fancy or complicated. You’re going to need Docker for this, and the installation instructions are over here for Ubuntu

Once you have Docker setup you just need the official Jenkins docker image from over here

docker pull jenkins

docker run -p 8080:8080 -p 50000:50000 jenkins

You can now access Jenkins at localhost:8080

We need to install the git plugin next, in the dashboard page on the left click Manage Jenkins -> Manage Plugins -> Available and install Git plugin and from the list.

After that let’s create a new job from the dashboard. Give it a name and pick Freestyle project.

At the Source Code Management section, type in the repo’s url and add your credentials.

2016-02-21-121721_1366x768_scrot In the Build Triggers we check Poll SCM and type in H/5 * * * *, this checks the repo for any changes every 5 minutes.

2016-02-21-123417_1366x768_scrotLastly in the Build section we’re going to execute a command to run the tests in the repo so from the drop down pick Build then type in command to run the tests

python -m unittest discover  -v

2016-02-21-123747_1366x768_scrotSave all changes and head back to the dashboard.

Now that’s done, try pushing a commit to the remote repo. After a few minutes a second build should have started because of a SCM change. 2016-02-21-130727_1366x768_scrot

If you click Console on the left, you can can see that the tests have run successfully.


Testing in Python – unittest


TDD seems to be the fad these days and I can see why,  so now would be a good time to learn up on automated testing and CI. How TDD works is that you write the unit test, the test should fail when you run it and then you write the piece of code to make the test pass. It gets pretty annoying to debug the code after each deployment, save yourself the trouble and write tests for them. There are a lot of testing frameworks (some with a more simple syntax than unittest) out there for Python, but I’ll be sticking to unittest.

Let’s write tests for our addition module from the last post.

Create a new file called in the same folder as the addition module.

import unittest
import addition

class TestAddition(unittest.TestCase):

    def test_add2(self):

    def test_add3(self):

if __name__ == '__main__':

In the first two lines you need to import unittest  and the module that is to be tested.

To create test cases you first need to create a class extending unittest’s TestCase class, usually it’s one test class for each class.

Next write methods to test each function, the method name has to start with test so the test runner knows it’s a test.

Here we use the assertEqual() to make sure that the value from the function we’re testing is the expected one. There are other functions we can use such as assertTrue() or assertFalse() to check if the output of the function is correct.

We add the unittest.main() at the end to run all the tests in the file. To run the test we do run the following from the command line.


If everything goes well then we should get something like

Ran 2 tests in 0.000s


There are other neat things you can do with unittest, more to read over here

Modules & Packages in Python

Modules & Packages

A module is a python source file containing function definitions.

moduleSo if I wanted to group a bunch of similar functions together and be able to use it anywhere else I’d create a module. For example, say I want to create a module that has two functions – add2() and add3(). Then I would need to create a new python file and call it and place both functions into it. From the interpretor I can import the module like so

>>>import add

and call the functions





Now a package in python is a group of modules and an initialization file in a folder. + = package

Let’s create an arithmetic package, for this we need to create a folder called arithmetic, inside it place the modules you want and file. The file is for the initialization of the package which can be empty for now.


If I want to use the arithmetic package, I can do so by importing it

>>>from arithmetic.addition import add2



You can read up more about modules and packages from over here

© 2019 Anna Philips

Theme by Anders NorénUp ↑