by: Achmad Fajar Ridwan, Mitrais Programmer

Automate Your Unit and Integration Testing with Jenkins

Have you ever been assigned as a code reviewer in your development team? How do you validate every change made by the developers? And how do you ensure that the changes are working and do not break the application?

Unit testing and integration testing are the tools that you need. However, the problem is how if there are a lot of Pull Requests opened in a single day? You have to run the testing for each PR, right? After that, you will be confident to merge it to the master branch. This process will take time and hinder productivity.

This article will give you a workaround on how to automate the unit testing and integration testing, thereby saving your time and making your work more efficient. We will use Github as a code repository and Jenkins as a Continuous Integration Tool.

The Scenario

Scenario to Automate Unit and Integrate Testing with Jenkins

In this scenario, we will utilize the Jenkins server to run the unit testing and integration testing automatically whenever there is a Pull Request created on Github and when the testing has finished, Jenkins will report the result to Github. Therefore, the reviewer only needs to evaluate the result from Jenkins. If everything is good then you can accept and merge it to the master branch. NO NEED FOR MANUAL TESTING at all.

Configurations

First of all, we have to install Jenkins (skip if you already have it) and do the following:

  1. Install Pull Request Builder plugin
  2. Create a Jenkins job for Unit Testing
  3. Create a Jenkins job for Integration Testing

After installing the plugin, the first thing that you need to do is to configure the github credentials on the plugin configuration. You can do that by clicking on Manage Jenkins > Configure System and find the Pull Request Builder section. Tips: remember to make the repo owner the user that connects to Github (or setup proper Push/Pull permissions).

Assuming that you already have jobs for Unit Testing and Integration Testing, to actually allow Jenkins to build your projects’ pull requests we'll need to configure it as follows:

  1. Open the job configuration page
  2. Find the Build Triggers section
  3. Tick the Pull Request Builder option.

Another important thing is to set the repository url on the Project URL field.

Repository URL on Project URL Field

GitHub Pull Request Builder

For more detail you can refer to the documentation on the official site.

Results

As I mentioned before, the final result of this integration is that the test result should be displayed on the Github Pull Request page. The following shows an example of a test result reported from Jenkins on the PR page.

Test Report from Jenkins

Now, you can try to create a Pull Request and see how it goes.

Conclusions

Every time a user makes or changes a Pull request, Jenkins will automatically run your tests and send the report to Github. Now, you as a reviewer can focus on other more important tasks instead of running the testing.

However, bear in mind that there is a limitation when using the Pull Request Builder plugin; the plugin doesn’t support Jenkins Pipeline, or use another workaround by using Github Organization which will make your Jenkins more complex.

Happy coding!