5

I tried to find this answer but hardly found it anywhere. I am doing the API testing, In process I need to call the rest API from my local machine. local machine contains the maven project and a framework to call respective rest API.

I need to check the code coverage of remote Rest API and form a report based on the code coverage. please help, how to do that?

Note: I found this link useful but it does not elaborate clearly on what to do?

http://eclemma.org/jacoco/trunk/doc/agent.html

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
atul tripathi
  • 239
  • 4
  • 14

1 Answers1

3

you will probably do a bit of file copying around - depending on the way you run the tests.

JaCoCo runs as a java agent. So you usually add the javaagent parameter as mentioned in the docs you linked to the start script of you application server.

-javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]

so it would look like:

java -javaagent: -jar myjar.jar

Using tomcat you can add the "-javaagent" part into JAVA_OPTS or CATALINA_OPTS environment variables. Should be similar for other servers.

this will create the jacoco*.exec files. you need to copy those back to your build or CI server to show its results (for ex if you use sonar you need those files before running the sonar reporter). Its important to just include the packages you're interested in.

You can also create one jacoco.exec file per test flavour (jacoco.exec for unit tests, jacoco-it.exec for integration tests, jacoco-at.exec for application tests).

And I would not mix coverage with performance testing - just to mention that too.

There are some examples on stackoverflow for JBoss

Community
  • 1
  • 1
wemu
  • 7,952
  • 4
  • 30
  • 59
  • Hi, I tried with given example but there is no file generated on server end, is there something which I am missing? – atul tripathi Oct 19 '15 at 17:56
  • did you stop the vm? the "output" parameter has a default set to "file" which is written on vm termination (into the "destfile" parameter, that file needs to be writeable by the user running the vm). – wemu Oct 20 '15 at 06:40
  • Hi, Thanks for your constant help ..Just for checking the file generation and all I have made a local server and client machiene, with file parameter, I am able to get the exec file but this contains nothing and equal to 0 byte. – atul tripathi Oct 20 '15 at 08:10
  • These are my Java_opts: -javaagent:C:\home\jacoco-agent.jar=destfile=C:\home\jacoco.exec,output=file,includes=restProject\src\main\java\**,address=*,append=true,dumponexit=false,classdumpdir=C:\home – atul tripathi Oct 20 '15 at 08:11
  • I think the includes pattern is not correct. Jacoco at this point will see the classes in the classpath - it does not know where the sources come from. So I guess it should be something like ...,includes=*,... or includes=com.domain.package.* to match the actual classes you're interested in – wemu Oct 20 '15 at 09:50
  • Hi, Your advice is really valuable and I am able to generate the *.exec file now. I have certain roadblocks though.. 1)how is it possible to generate the *.exec without shutting down the server because I don't think its feasible to shutdown the server every time I need the report.. 2) Is there a way to pull this *.exec file back to y local system and then run the report generator.. this link has some utility that might actually do it: http://eclemma.org/jacoco/trunk/doc/api.html – atul tripathi Oct 20 '15 at 11:43
  • it depends :) since you measure coverage I think its possible to startup the server, measure, shut it down again, copy the files back, create a report. The output parameter allows you to specify a tcp connection where results are send to or can be read from. That might be better suitable for you. There are several ways to copy files around. scp, ftp, rsync. You may even be able to store the .exec files into the public webapp folder and download them via http. Really depends on the tools you are using. – wemu Oct 20 '15 at 12:00
  • Agreed with your point, but still since it requires some sort of effort to start and stop the server, just wanted to understand Is there any workaround to avoid server stop? and also what would be the effect on generated report if more than 1 people are hitting the same API at the same time? Will we see the conflict in report? – atul tripathi Oct 20 '15 at 14:45
  • hello. only if the output to the tcp address works in a streamed mode. I've not tried that but it seems likely. If several clients hit the server you will not see that. The coverage report does not care who called the code - only what code was called. So to not mess the reports up the server for coverage should not be the same one as for other tests or for manual testing. If several client call the same code: that will not matter. Only counts as one. – wemu Oct 20 '15 at 14:55
  • Thanks for very valuable information, just to summarize what I understood, I need to isolate the server for coverage so that other don't mess up with this and if "If several client call the same code: that will not matter. Only counts as one." -- means I will get the concrete report of total coverage by several hits, right? – atul tripathi Oct 20 '15 at 15:31
  • and also, please elaborate this point "if the output to the tcp address works in a streamed mode".. how can i actually achieve that? I understand I am asking several questions but please bear with, your help is highly appreciated :) – atul tripathi Oct 20 '15 at 15:32
  • My query is almost resolved, I retraced your answer and I need to clarity on one more point mentioned by you: "You can also create one jacoco.exec file per test flavour (jacoco.exec for unit tests, jacoco-it.exec for integration tests, jacoco-at.exec for application tests)." how can I achieve that? – atul tripathi Oct 20 '15 at 15:39
  • the file you create is configured through the "destfile" option. If you want to distinguish application integration tests from system integration tests you need to do two runs and specify a different output file. Maven does that for unit tests (surefire plugin) and integration tests (failsafe plugin) the same way. It runs separate test suites and creates different files. Since the tests may differ in their scope you probably do a different environment setup anyway. Just to mention: there is no requirement to do that. If you have only smoke tests running against the UI and an API do one file. – wemu Oct 21 '15 at 06:38
  • Thanks for useful information. But It gave me one more doubt, please help :) 1) when you mention about maven plugins, where do I need to configure these surefire and failsafe plugins, client side or server side? 2) and you missed one of my previous question: if the output to the tcp address works in a streamed mode? what did you mean by that.. Please help.. – atul tripathi Oct 23 '15 at 05:48
  • maven plugins are meant for maven builds. if you have a pom.xml in your project: in there. It measures coverage during the build itself - not its runtime, 2) the default for jacoco is to write a file on exiting the vm. if you change that and use a tcp stream you may be able to retrieve that information without exiting the vm. I've never tried that you need to do that experiment yourself :/ – wemu Oct 23 '15 at 06:44
  • :) definately I will try that, my only concern with POM.xml was, if I am configuring it in server side then it dont makes any sense since my project is already deployed, and I dont understand its need for my local project since I need the coverage for remote machiene. – atul tripathi Oct 23 '15 at 07:10
  • I see. Sorry for the confusion it was just meant as an example that you can measure the coverage of different tests flavors by putting the results in different files - but it requires on run per output file. In your case the maven stuff with its test plugins is not relevant. – wemu Oct 23 '15 at 07:57
  • @atultripathi, not sure if this thread is active, but i am still unable to generate my jacoco.exec. Tried stopping the server post the server as well, but the size remains as 0 bytes. Although i can see some testcoverage class file generated in the dest location but the file size of jacoco.exec remains as zero.. The arguments that i have given are -javaagent:C:\Test\Code\jacocoagent.jar=destfile=C:\home\jacoco.exec,output=file,includes=*,address=,append=true,dumponexit=false,classdumpdir=C:\home – Ankit Jul 31 '19 at 12:13