如何计算关于Web应用程序代码的selenium测试的代码覆盖率 [英] How to calculate code coverage of selenium tests with respect to web application code

查看:120
本文介绍了如何计算关于Web应用程序代码的selenium测试的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求将我的selenium测试的代码覆盖率捕获到所涵盖的服务器代码(Web应用程序源代码)中的源代码量。

I have a requirement to capture the code coverage of my selenium tests to the amount of source code in the server code (web application source code) covered .

例如,登录功能的测试应捕获Web应用程序中为登录功能所涵盖的代码量。

For example the tests for login functionality should capture the amount of code covered in web application for the login function.

否则需要知道在完成方案的Web应用程序代码中触及了哪个包或类。例如登录

Else need to know which package or class it has touched in web application code for a scenario done . Eg a login

我找不到合适的解决方案,虽然我开始了解Jacoco代码覆盖工具并尝试使用Jacoco Jenkins插件的一些示例,但是没有成功。

I couldn't find a suitable solution , although i came to know about Jacoco code coverage tool and tried some samples with the use of Jacoco Jenkins plugin , but there is no success .

我不知道可能性。请给我一个如何实现这一目标的建议,在此先感谢

I am not aware of the possiblity. Please provide me a suggestion on how to achieve this , Thanks in advance

推荐答案

确保你这样做。请注意,如果您使用的是Gradle,Maven或ANT。但是以下概念与任何构建系统非常相似。

Make sure you do this. Note sure if you are using Gradle, Maven or ANT. But the following concept is very similar to any build system.


  1. 您必须拥有所需的.war / .ear应用程序工件跑在Tomcat /类似的后面。

    • 您还需要确保在调试模式下编译主代码,否则jacoco将不满意。

    • 对于ex :Java中的-g选项和类似的调试选项(如果你使用的是groovy)。


tasks.withType(Compile) {
    options.debug = true
    options.compilerArgs = ["-g"]
}



  1. 假设您拥有.war并使用Tomcat。然后在启动Tomcat时。

    • 在Tomcat启动脚本中,确保告诉Tomcat的JVM jacocoagent.jar文件并传递其他参数。这是我们看到的主要缺失点(也就是没有将jacoco附加到目标JVM的会话并尝试获取代码覆盖率)。

例如:我使用传递给Tomcat的以下参数启动我的Tomcat脚本(-Dxxxx =值方式)

For ex: I start my Tomcat script with the following parameter passed to Tomcat (-Dxxxx=value way)

PROJ_EXTRA_JVM_OPTS=-javaagent:tomcat/jacocoagent.jar=destfile=build/jacoco/ST/jacocoST.exec,append=false

基本上,Tomcat启动脚本会有-Dparameter = value,你可以将上面的参数(Linux / Unix export 变量)传递给Tomcat / Target JVM范围。

Basically, Tomcat start script would have -Dparameter=value, you can pass the above parameter (Linux/Unix export the variable) to Tomcat/Target JVM's scope.

上面的参数行发送到Tomcat时,会将JACOCO代理.jar文件附加到TARGET(又名Tomcat JVM)。在这里,您要告诉Tomcat,从您工作区下面的一个名为tomcat的导向器中查找jacocoagent.jar文件。它将在build / jacoco / ST文件夹下创建一个名为jacocoST.exec(也称为Selenium Test的jacoco exec文件)的jacoco .exec文件(我正在使用Gradle,因此Gradle会在您运行构建/编译时创建build文件夹/ test / integrationTest / customSeleniumTaskThatYouMightHaveCreated)。

The above parameter line when sent to Tomcat, will attach JACOCO agent .jar file to the "TARGET" (aka Tomcat JVM). Here you are telling Tomcat that go look for jacocoagent.jar file from a director called "tomcat" under your workspace. It's gonna create a jacoco .exec file named "jacocoST.exec" (aka jacoco exec file for Selenium Test) under build/jacoco/ST folder (I'm using Gradle so Gradle creates "build" folder anytime you run a build/compile/test/integrationTest/customSeleniumTaskThatYouMightHaveCreated).

注意:这意味着,您不必在测试任务中指定jacoco部分(因为这将在您的构建系统的JVM Gradle或Maven或ANT,无论你有什么)。

NOTE: This means, that you DON'T have to specify jacoco section in the test task (as that'll run in your BUILD systems' JVM either Gradle or Maven or ANT whatever you have).

//We don't need jacoco for non-unit tests type of tasks as Jacoco won't be able to find any coverage if done this way. Jacoco agent file needs to be attached/visible to the TARGET's JVM (where you run your application via a .war / .ear etc).

jacoco { 
  //  ... As Gradle runs Unit tests (while doing build), they run free, in the same JVM where Gradle runs the build so Unit test have visibility to the main classes in the same JVM (which Gradle is using to run the build). Thus, you can use jacoco section in Gradle for running unit tests. BUT,
  // ... Don't use this section for running Integration, Acceptance, Selenium tests which run on a target JVM. Instead attach jacocoagent.jar and specify jacoco parameters to the target JVM.
}




  1. 一次你已经启动并运行了Tomcat,现在你运行了Selenium测试。
    注意:
    - 我在Linux / Unix机器上使用Jenkins而且xvfb插件非常方便,即现在我可以在HEADLESS模式下运行Selenium GUI测试而且我不会对任何用户造成错误通过在GUI测试运行时弹出测试页面来运行测试的机器。

  1. Once you have your Tomcat up and running, now you run your Selenium tests. NOTE: -- I'm using Jenkins on Linux/Unix machine and "xvfb" plugin is very handy i.e. now I can run Selenium GUI tests in HEADLESS mode and I won't bug any user on a machine where the tests are running by popping up the tests pages while the GUI tests are running.

- 如果你最终使用 xvfb Jenkins中的插件, FIRST 需要在运行测试的服务器(Linux / Unix)上启动 Xvfb 服务。

-- if you end up using "xvfb" plugin in Jenkins, you FIRST need to start "Xvfb" service on the server (Linux/Unix) where you are running the tests.

- 如果您在Windows机器上运行非单元测试(也称为Integration / Selenium等),那么您可以在运行测试时看到GUI测试弹出。如果您不想看到弹出窗口,那么您的Jenkins实例可以将从属(您的Windows机器)进程作为服务运行(作为服务安装)。如果您将Windows计算机创建为从属设备,当您在计算机上运行JLNP安装时,您将看到Jenkins已成功启动从属进程的弹出窗口,单击文件>安装即服务将在Windows计算机上运行您的从属服务器作为HEADLESS。

-- If you are running your non-units tests (aka Integration/Selenium etc) on a Windows machine, then you can see the GUI tests pop up when you run your tests. If you don't want to see the popup windows, then your Jenkins instance can run the slave (your windows machine) process as a service ("Install as a Service"). If you create your windows machine as a slave, when you run the JLNP installation on your machine, You'll see a popup that Jenkins has successfully started a slave process, clicking File > Install as a service will run your slave on a windows machine as "HEADLESS".

当您的测试正在运行时,您会注意到,这次,jacoco将根据您的定义创建文件夹结构/ exec文件 destfile 参数的值,但它仍然是0或一些小的。

While your tests are running, you'll notice that this time, jacoco will create a folder structure/exec file as per your defined value for destfile parameter but it'll still be 0 or some small size.

一旦你的Selenium /非单元测试是完成后,你必须停止Tomcat /目标JVM。这将把所有jacoco覆盖信息刷新到这个jacocoST.exec文件(您希望jacoco创建的自定义文件)。
- 注意:如果你想在运行时刷新jacocoST.exec文件(不需要停止Tomcat JVM /会话,那么你可以查看jacoco文档如何做到这一点,那里有一个主题告诉你这个,这样你的应用程序可以继续运行,你不必停止你的应用程序/ web服务。)

Once your Selenium/non-unit tests are complete, you have to "STOP" Tomcat / target JVM. This will FLUSH all jacoco coverage info to this jacocoST.exec file (custom file that you wanted jacoco to create). -- Note: If you want jacocoST.exec file to be flushed on the fly (without requiring the Tomcat JVM/session to stop, then you can look into jacoco documentation how to do that, there is one topic there which tells about this, this way your application can continue to run and you dont have to stop your application/webservice).

运行jacocoTestReport任务,你会看到jacoco代码覆盖率。

Run jacocoTestReport task and you'll see jacoco code coverage.


  • 确保指定主代码的来源/类的位置。

ex:

  jacocoTestReport {
      group = "Reporting"
      description = "Generate Jacoco coverage reports after running tests."
      ignoreFailures = true


      //UT=is for Unit tests, IT=integrationTest, AT=acceptanceTest, ST=Selenium GUI tests.
      //executionData = files('build/jacoco/UT/jacocoUT.exec')
      //executionData = files('build/jacoco/IT/jacocoIT.exec')
      //executionData = files('build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec')

      //executionData = files(['build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec'])
      //OR use the following way for all.
      executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

      reports {
             xml{
                 enabled true
                 //Following value is a file
                 destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
             }
             csv.enabled false
             html{
                 enabled true
                 //Following value is a folder
                 destination "${buildDir}/reports/jacoco/html"
             }
      }

      //sourceDirectories = files(sourceSets.main.allJava.srcDirs)
      sourceDirectories = files('src/java')
      //sourceDirectories = files(['src/java','src/groovy'])
      classDirectories =  files('build/classes/main')

      //------------------------------------------
      //additionalSourceDirs = files(['test/java','test/groovy','src/java-test', 'src/groovy-test'])
      //additionalSourceDirs += files('src/java-test')
}

如果您仍然发现任何问题,请随时给我打电话。您还可以在stackoverflow上看到我的一些帖子,关于我如何实现这一点,并将相同的报道发布到SonarQube。

Feel free to ping me if you still see any issues. You can also see few of my posts here on stackoverflow on how I achieved this and also publishing the same coverage to SonarQube.

这篇关于如何计算关于Web应用程序代码的selenium测试的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆