Generate code coverage using Jacoco with Sonarqube | [Latest 2022]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my channel today in this video we will see how we can generate a test coverage report for a gradle project using jacoko and then we will enable sonarqube to read this test coverage report and and display it on the sonarqube dashboard this is going to be an addition to our sonarqube playlist i have already made a video on sonarqube setup and how to analyze our code base using sonar cube if you haven't checked that video yet you can watch it using the i button or from the link in the description so without any further ado let's start with this video in this video today we will see how we can use jacoko with sonar cube we will also see how we can generate a test coverage report for our code base and then how we can enable sonarqube to read test coverage report and we will also verify the code coverage on sonarqube dashboard so first i'll show you the code base uh which we have used in our last video or where we have done the sonar cube setup so i'll show you so we have this project called snake and ladder so we have few classes written over here okay along with this we also have a test package where i have few tests around this classes what i'm going to do next is i'll show you the sonar dashboard which we have created in the last video so this is that dashboard for the snack and letter code base so here on the overall code we have few information like number of bugs vulnerabilities security hotspots then the number of quotes means we have on our code base and then we have the information about the tech depth effort which is 31 minute and we also get the information about the number of unit test you have in your code base along with this we have coverage information this value is right now zero percent because though we have test present in the code base but sonarqube is not able to read the information about the coverage okay so for that we will be using jakoku which will be generating a report for that and by reading that report sooner you should be able to tell the coverage of your code base okay that's a brief about this sonar cube dashboard uh we will quickly jump to the process so what we need to do uh i'll go to the code base first so here we need few things to enable jacoko first so in the build.gradle so as we are using our gradle project so the first thing we need to do here is we need to add the jacoku plugin so that we can generate coverage report so the first thing i'll do i'll simply add a jacoku plugin so you can simply add id jacobo in the plugin section and this will add the jacoho plugin so once we add the jacoku plugin uh there is a task called jacoko test report which gets created automatically so we simply need to add this line finalized by jacobo test report under the test task so that this task will generate the test reports and then we can configure this gradle task to run after the test so for that you need to add this snippet so here under jaku test report what i'm saying is it depends on tests so report generation will wait until your test task completes and then only it will create the report okay so here by default you will get a html report but to enable the xml report you have to add this line where under reports you just need to pass xml dot enabled true okay and this will create your xml report here one thing to notice is though we are getting html test report by default we still need to add xml based test report because sonar cube supports xml reports okay so once we add this configuration the next thing we can do is we can simply mention the version of the jacoko so here i'll be using 0.8.7 version okay so that's it that's about the configuration part once your configuration is done you simply refresh your gradle let all the changes be loaded so this is all about the build.gradle file configuration now the next thing you need to do is you simply go to your sonarqube directory so here under sonarqube directory we have this conf folder okay so i have already uh shown this uh different folders and usages in my last video if you haven't seen that video you can watch it from the i button or from the link in the description uh there you will also get to know how we have uh set up the entire sonarqube server from scratch and also how we have analyzed a code base using sonar so those thing you will get to know from that video for now i need to show you one more property which we need to add to this sonar properties file so that your test coverage report can be generated on the sonarqube dashboard so for that uh let's go at the end of this file so here if you see i have added a property sonar jacob report path and this is build reports jaco code test and then the xml file so here you have to add this path of your build folder where your xml will get generated so by default if you don't specify any path in the configuration it simply generates your xml file here okay i'll also show you uh in the project structure where it gets generated so for that i need to build the project so i'll simply save this configuration for now okay and then uh all we need to do is we need to run the sonar cube command okay so in the last video i've shown you that uh we used to run this command where we have this gradle sonar cube with some properties so i have also shown these properties uh on my first video where we have set up the server and then we ran the sonar cube and then we analyzed the code so there i have shown how you get this command from the sonarqube dashboard only so for now i'll skip that part i'll simply show you after all the configuration and properties you just simply need to run this command again so that your test and your jakoku task will get executed again and everything will get loaded on your dashboard so let this complete so now this build is successful now we can go to our sonar cube dashboard so here we are earlier this was showing that uh the coverage was zero percent let's refresh this so i'll simply refresh the dashboard so yeah here if you see now this got updated to point three nine percent right and if you click here you can see this thing in details so here you will see different files you can go to them and you can simply check the files right here you will simply see that uh the green ones are the lines which are covered and the red ones which are not covered so in this way you can also increase your coverage by just by checking which line needs to be covered and you can simply go ahead and add the relevant test and your coverage will come up so yeah that's about the test coverage where sonar cube gives you a detailed analysis of your code base uh using which you can simply get to know which all lines needs to be covered so yeah that's about the sonar cube uh test coverage so now let's also check the code base where we have generated the report so here uh so under the build folder we have this folder reports under which we have jacopo so here we have jacoko test report or for which we have provided the path in the sonar properties and similarly if you click on this you will see this a xml report and if you go to html folder you will see as i mentioned that we have a default report in html so you can simply check this html on a browser so here on the browser you will see a detailed coverage report of your code base so here you can see the all the percentages and then there are different attributes you can go through for now i'll simply go to the package and inside this you have access to different classes if you click on any of the classes you should be able to see the exact code of your code base and then or anything which is marked with green that means it is covered with test and anything which is marked with red is not covered so let's go to this so if you see there is this ladder factory uh written on our code base which are not covered with tests so in this way you can get the detailed report of your code base and this reports will definitely help you to improve your code coverage so that's about the coverage report so yeah that's about the code coverage using jacoko with sonarq so if you find this video helpful please like this video and share this video with your friends and also if you want me to make specific topic or related to sonar cube then please let me know in the comment section i will be happy to make those videos so yeah that's it for today at the end i would like to give you a walkthrough of our channel so here if you go on the home page it looks something like this and if you go under video section you will get to see the different videos which i have uploaded till now so there are a lot of topics i have covered so if you feel that any of them could be useful to you go ahead and watch it and if you have any suggestion or advice about the videos or any feedback feel free to let me know in the comment section that's all about this video if you learned something from this video please hit the like button and if you're new to this channel please don't forget to hit the subscribe button and press that bell icon so that you get notified for our upcoming new videos thank you for watching
Info
Channel: Engineerhoon
Views: 27,540
Rating: undefined out of 5
Keywords: code coverage, jacoco, sonarqube, analyse code quality using SonarQube, sonarqube tutorial, analyse code quality sonarqube, code quality analyser, test coverage using sonarqube, code quality, static code analysis, java code coverage, jacoco code coverage tutorial, code coverage testing, code coverage using jacoco, code coverage in sonarqube, jacoco test reports, enable jacoco for gradle project, jacoco plugin, jacoco configuration, sonarqube setup, sonarqube property jacoco
Id: zM2DOalHxHY
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.