zoukankan      html  css  js  c++  java
  • 测试框架:使用SONAR分析代码质量

    测试框架:使用SONAR分析代码质量

     
    介绍

    Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd、checkstyle、findbugs、Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。 

     
    SONAR安装&运行
     
    运行:解压后,根据平台运行bin下不同目录下的启动脚本。对于linux x86_64,运行bin/linux-x86-64/sonar.sh。
    可用命令:
    ./sonar.sh { console | start | stop | restart | status | dump }
     
    安装插件:
    将插件放置在${SONARHOME}/extensions/plugins下,重启sonar后生效。注意版本号要匹配。本例中,SonarQube版本为4.4,所以选择插件版本为1.8的。
      
    SONAR + Maven分析代码质量
    1)设置sonar使用的数据库信息。
    本例设置sonar使用mysql数据库存储分析数据。保存设置后,执行restart使其生效。
     
    ${SONARHOME}/conf/sonar.properties:
    1. # Permissions to create tables, indices and triggers must be granted to JDBC user.
    2. # The schema must be created first.
    3. sonar.jdbc.username=root
    4. sonar.jdbc.password=root
    5. # Comment the following line to deactivate the default embedded database.
    6. #sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
    7. #----- MySQL 5.x
    8. # Comment the embedded database and uncomment the following line to use MySQL
    9. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
     
    2)需要在Maven的settings.xml设置sonar信息。
    其中<sonar.host.url>http://localhost:9000</sonar.host.url>指明了sonar服务器的地址。所以在执行maven命令的时候,<sonar.host.url>指明的服务器必须已运行起来。
     
    ${MAVEN_HOME}/conf/settings.xml:
    1. <profiles>
    2.     <profile>
    3. <id>sonar</id>
    4. <properties>
    5. <sonar.jdbc.url>jdbc:mysql://192.168.198.128:3306/sonar</sonar.jdbc.url>
    6. <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
    7. <sonar.jdbc.username>root</sonar.jdbc.username>
    8. <sonar.jdbc.password>root</sonar.jdbc.password>
    9. <sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Sonar服务器访问地址 -->
    10. </properties>
    11.     </profile>
    12. </profiles>
    13. <activeProfiles>     <activeProfile>sonar</activeProfile>
    14. </activeProfiles>
    3)执行mvn sonar:sonar命令进行代码分析。
    我们可以在Eclipse中,对一个标准maven工程执行sonar。说明:由于maven对sonar有很好的支持,会自动执行相应的脚本,所以无需在pom中添加sonar说明。
    在执行maven进行sonar分析之前,必须确保sonar服务器已经处于运行状态。本例中sonar服务器运行在localhost:9000上。
     
    首先,执行sonar:sonar命令,最后得到输出如下输出。如果输出”BUILD SUCCESS“说明已经构建成功。
    然后,我们可以在浏览器查看分析结果。
     
    查看分析结果
    对于使用sonar自带服务器来说,在浏览器访问:http://sonar_ip:9000,打开sonar结果页面。可使用admin/admin账号登录进入。
    1)home页
    下面是home页,右边PROJECTS页面列出了所有的工程。点击红色框内的链接,可以查看详细情况。

     
    2)工程总面板视图
    Dashboard包含了很多信息,比如程序统计信息、问题统计信息、技术债务、代码复杂度、单元测试覆盖度等。

     
    3)Hotspots热点区
    在热点区,可以查看比较主要(hot)的信息。

     
    4)问题视图
    点击左侧导航树的“问题”,打开问题视图页。通过点击问题数,如下红框所示,可以查看具体问题。
     
    点击问题数后,进入具体问题页。SonarQube允许管理员对问题进行重新确认,比如可以认为一个打开的问题是误判的。

     
    下面是认为一个问题是误判后的情况。

     
    在问题页面,可以通过“状态”搜索问题。下面是搜索“误判”问题的结果。

     
    5)技术债务
    这里列出了修复问题所需要的时间,所谓技术债务。出来混总要还的,遗留的问题越多,技术债务越大。

     
    6)问题明细
    这里列出问题明细,包括问题严重级别,对应的问题数量,问题的描述。
  • 相关阅读:
    jmeter函数 助手
    虚拟机如和 连接网络
    Loadrunner如何进行有效的IP欺骗
    JMeter生成HTML性能报告
    关于interrupt(),interrupted(),isInterrupted()用法分析
    我的博客----我的大学
    基本排序算法总结
    多个线程之间的通信问题
    多线程同步问题
    第三十七章 : 奇珍异宝
  • 原文地址:https://www.cnblogs.com/TestMa/p/10345075.html
Copyright © 2011-2022 走看看