by: Achmad Fajar Ridwan, Programmer at Mitrais
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.
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.
First of all, we have to install Jenkins (skip if you already have it) and do the following:
- Install Pull Request Builder plugin
- Create a Jenkins job for Unit Testing
- 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:
- Open the job configuration page
- Find the Build Triggers section
- Tick the Pull Request Builder option.
Another important thing is to set the repository url on the Project URL field.
For more detail you can refer to the documentation on the official site.
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.
Now, you can try to create a Pull Request and see how it goes.
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.