Python Unit Tests on Jenkins

thumbnail for this post

Hi devops, we installed Jenkins on docker in this post. Now, i wonder how we can run my unit tests in Nose framework with Python language.

You know what the unit test is. Unit Testing is a software test stage where a program measures individual units / components. The aim is to validate the output of each unit of the program as expected. A unit is any software's smallest testable component. Usually it has one or several inputs and usually a single output.And you know why Python language is choosen for this post. It is the third popular language in the world according to TIOBE

In this tutorial, you will learn:

  • Integrating Jenkins with GitHub
  • Running Unit tests with Nose framework with Python code

Integrating Jenkins with GitHub

First of all, if you don't have Git Plugin please check Prerequisits part of this post. You have it if you followed this post

Let's create a project with “New Item”. Name it “test1” and make it “Freestyle Project”.

After creating project, we can configure it.

Git->Source Code Management My project will use public github repo. It is a fresh and simple nosetests ready Python project. I find it from web and forked it in order to keep it for a long time. Python Unit Test Example. We can put this repo to newly created item in Jenkins in “Source Code Management->Git->Repository Url”. It will check automatically and accept if everything is ok.

Running Unit tests

We want to run unit tests. So, in the Build section we just click “Add Build Step->Execute Shell” and paste the code below.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user
PATH=$WORKSPACE/venv/bin:$HOME/.local/bin:$PATH

if [ ! -d "venv" ]; then
		pip install virtualenv --user
        virtualenv venv
fi
. venv/bin/activate
pip install -r requirements.txt
nosetests $WORKSPACE/tests

If you “Save” and “Build Now” from left menu, you can see that Jenkins gets the code from repo, and then runs our script and get a success.

Let's look at the process. Our shell script gets pip. Then python3 installs it. We export PATH variable to be able to run pip. Then if venv not activated, if necessary install virtualenv and activate. After that, pip will install the packages you specify on requirements.txt. Then at last, finally :), run units tests. Hopefully, there is a success. Please examine the repo. It just sums 1 and 2 and checks whether it is 3 or not :)

As an example you can fork the repo and change the 3 to 4 and see build failure.

In our blog, we will soon use pytest package in the near future.

We have learned how to make unit tests on Jenkins with Python and Nose. With just a few clicks, now we know whether our functions work or not. Think about a huge project with hundreds of functions. May be they use eachother. And a developer changed one of them and make your code broke. Now, you know this as soon as your first commit :)

We will integrate integration tests, load test and security test after successful builds and deployments with Testbone.

Prerequisits

Install GIT Plugin

Manage Jenkins Manage Jenkins

Manage Plugins Manage Plugins

Git Plugin GIT Plugin

Tool Tips

  • If you see your local language and you are not satisfied, you can change it to English with installing Locale plugin. Then you have to write “en_US” in “Configure System->Locale->Default Language” and check Ignore
  • Delete docker container: docker kill container_id docker rm container_id
  • Delete docker volume: docker volume rm jenkins_home
  • Delete all orphand volumes docker volume prune

Dictionary

  • Jenkins is the leading open source automation server, provides hundreds of plugins to support any project's construction, deployment and automation. It helps developers of software to continually build and test projects. This method is used for continuous integration and automation of any activities related to software testing, design or deployment.[1]
  • Nose is the predecessor to nose2. It’s unittest with plugins. nose2 is a new project and does not support all of the features of nose. See differences for a thorough rundown.nose2’s purpose is to extend unittest to make testing nicer and easier to understand. nose2 vs pytest. nose2 may or may not be a good fit for your project. If you are new to python testing, we encourage you to also consider pytest, a popular testing framework.

References