51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2653|回复: 2
打印 上一主题 下一主题

白盒自动化测试工具:FindBugs的使用指南

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:19
  • 签到天数: 933 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-11-22 13:16:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     1、FindBugs介绍
      FindBugs是一款Java静态代码分析工具,与其他静态分析工具(如Checkstyle和PMD)不同,FindBugs不注重样式或者格式,它专注于寻找真正的缺陷或者潜在的性能问题,它可以帮助java工程师提高代码质量以及排除隐含的缺陷。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。
      FindBugs运用ApacheBCEL库分析类文件(class文件)而不是源代码,将字节码与一组缺陷模式进行对比以发现可能的问题。FindBugs的检测器已增至300多条,被分为不同的类型,常见的类型如下:
      正确性(Correctness):这种归类下的问题在某种情况下会导致bug,比如错误的强制类型转换等。
      最佳实践反例(Badpractice):这种类别下的代码违反了公认的最佳实践标准,比如某个类实现了equals方法但未实现hashCode方法等。
      多线程正确性(Multithreadedcorrectness):关注于同步和多线程问题。
      性能(Performance):潜在的性能问题。
      安全(Security):安全相关。
      高危(Dodgy):FindBugs团队认为该类型下的问题代码导致bug的可能性很高。
      2、在Eclipse中安装FindBugs插件
      以下为我的安装步骤,可能大家用的是MyEclipse步骤不仅相同。
      Step1:

    Step2:

    Step3:

     3、在Eclipse中使用FindBugs操作步骤
      3.1打开FindBugs视图
      step1:

     step2:

     step3:

    3.2执行FindBugs任务
      右键单击你要检测的工程、包或文件-->FindBugs-->FindBugs。

     check完成后将在BugExplorer视图中看到问题列表,该列表以问题类型组织。


    展开列表,双击列表中具体的问题就可以定位的具体的代码行。


    4、配置FindBugs
      在这里可以对FindBugs规则等进行详细设置。选择你的项目,右键-->Properties-->FindBugs-->

    4.1 RunAutomatically开关
      当此项选中后,FindBugs将会在你修改Java类时自动运行,如你设置了Eclipse自动编译开关后,当你修改完Java文件保存,FindBugs就会运行,并将相应的信息实时显示。
      当此项没有选中,你只能每次在需要的时候自己去运行FindBugs来检查你的代码。
      4.2 DetectorConfiguration选择项
      在这里你可以选择所要进行检查的相关的BugPattern条目,你可以根据需要选择或去掉相应的检查条件。

    4.3 Minimumprioritytoreport选择项
      这个选择项是让你选择哪个级别的信息进行显示,有Low、Medium、High三个选择项可以选择,很类似于Log4J的级别设置啦。比如:
      High选择项:只有是High级别的提示信息才会被显示;
      Medium选择项:只有是Medium和High级别的提示信息才会被显示;
      Low选择项:所有级别的提示信息都会被显示。
      4.4 Reportbugcategories选择项
      在这里是一些显示Bug分类的选择:
      Maliciouscodevulnerability关于恶意破坏代码相关方面的
      Correctness关于代码正确性相关方面的
      Internationalization关于代码国际化相关方面的
      Performance关于代码性能相关方面的
      Multithreadedcorrectness关于代码多线程正确性相关方面的
      5、Ant使用简介
      有了Ant这样的自动化机制,就可以以多种方法监视软件质量。有许多种扫描源代码和二进制文件的工具,但是其中最流行的两种是PMD和FindBugs。PMD扫描源代码文件,并根据一系列规则检查其中的代码。如果代码有问题,它就会报告一个违规。FindBugs的作用与PMD相似,但是它扫描二进制文件(即类文件)并报告bug。
      以下为配置脚本:
    1. project name="analyze_asm_util" default="findbugs">

    2.      <!-- findbugs task definition -->
    3.      <property name="findbugs.home" value="/Users/ben/Documents/workspace/findbugs/findbugs" />
    4.      <property name="jvmargs" value="-server -Xss1m -Xmx800m -Duser.language=en -Duser.region=EN -Dfindbugs.home=${findbugs.home}" />

    5.       <path id="findbugs.lib">
    6.         <fileset dir="${findbugs.home}/lib">
    7.            <include name="findbugs-ant.jar"/>
    8.         </fileset>
    9.      </path>

    10.      <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
    11.         <classpath refid="findbugs.lib" />
    12.      </taskdef>

    13.      <taskdef name="computeBugHistory" classname="edu.umd.cs.findbugs.anttask.ComputeBugHistoryTask">
    14.         <classpath refid="findbugs.lib" />
    15.      </taskdef>

    16.      <taskdef name="setBugDatabaseInfo" classname="edu.umd.cs.findbugs.anttask.SetBugDatabaseInfoTask">
    17.         <classpath refid="findbugs.lib" />
    18.      </taskdef>

    19.      <taskdef name="mineBugHistory" classname="edu.umd.cs.findbugs.anttask.MineBugHistoryTask">
    20.         <classpath refid="findbugs.lib" />
    21.      </taskdef>

    22.      <!-- findbugs task definition -->
    23.      <target name="findbugs">
    24.         <antcall target="analyze" />
    25.         <antcall target="mine" />
    26.      </target>

    27.      <!-- analyze task -->
    28.      <target name="analyze">
    29.         <!-- run findbugs against asm-util -->
    30.         <findbugs home="${findbugs.home}"
    31.                   output="xml:withMessages"
    32.                   timeout="90000000"
    33.                   reportLevel="experimental"
    34.                   workHard="true"
    35.                   effort="max"
    36.                   adjustExperimental="true"
    37.                   jvmargs="${jvmargs}"
    38.                   failOnError="true"
    39.                   outputFile="out.xml"
    40.                   projectName="Findbugs"
    41.                   debug="false">
    42.            <class location="asm-util-3.0.jar" />
    43.         </findbugs>
    44.      </target>

    45.      <target name="mine">

    46.         <!-- Set info to the latest analysis -->
    47.         <setBugDatabaseInfo home="${findbugs.home}"
    48.                               withMessages="true"
    49.                               name="asm-util-3.0.jar"
    50.                               input="out.xml"
    51.                               output="out-rel.xml"/>

    52.         <!-- Checking if history file already exists (out-hist.xml) -->
    53.         <condition property="mining.historyfile.available">
    54.            <available file="out-hist.xml"/>
    55.         </condition>
    56.         <condition property="mining.historyfile.notavailable">
    57.            <not>
    58.               <available file="out-hist.xml"/>
    59.            </not>
    60.         </condition>

    61.         <!-- this target is executed if the history file do not exist (first run) -->
    62.         <antcall target="history-init">
    63.           <param name="data.file" value="out-rel.xml" />
    64.           <param name="hist.file" value="out-hist.xml" />
    65.         </antcall>
    66.         <!-- else this one is executed -->
    67.         <antcall target="history">
    68.           <param name="data.file"         value="out-rel.xml" />
    69.           <param name="hist.file"         value="out-hist.xml" />
    70.           <param name="hist.summary.file" value="out-hist.txt" />
    71.         </antcall>
    72.      </target>

    73.      <!-- Initializing history file -->
    74.      <target name="history-init" if="mining.historyfile.notavailable">
    75.         <copy file="${data.file}" tofile="${hist.file}" />
    76.      </target>

    77.      <!-- Computing bug history -->
    78.      <target name="history" if="mining.historyfile.available">
    79.         <!-- Merging ${data.file} into ${hist.file} -->
    80.         <computeBugHistory home="${findbugs.home}"
    81.                              withMessages="true"
    82.                              output="${hist.file}">
    83.               <dataFile name="${hist.file}"/>
    84.               <dataFile name="${data.file}"/>
    85.         </computeBugHistory>

    86.         <!-- Compute history into ${hist.summary.file} -->
    87.         <mineBugHistory home="${findbugs.home}"
    88.                           formatDates="true"
    89.                         noTabs="true"
    90.                           input="${hist.file}"
    91.                           output="${hist.summary.file}"/>
    92.      </target>

    93.   </project>
    复制代码











    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-20 04:21 , Processed in 0.064403 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表