132.md 11.3 KB
Newer Older
W
wizardforcel 已提交
1
# 10b。 高级 WebDriver – 生成 JUnit 报告
W
init  
wizardforcel 已提交
2 3 4 5 6

> 原文: [https://javabeginnerstutorial.com/selenium/10b-advanced-webdriver-generating-junit-reports/](https://javabeginnerstutorial.com/selenium/10b-advanced-webdriver-generating-junit-reports/)

嗨冠军! 报告,报告,无处不在的报告。 从哈利·波特时代开始,我们就一直在处理这些问题!! (还记得学校的进度报告吗?!)无论如何,报告真的很重要,尤其是在测试中,以便快速了解一切工作原理。

W
wizardforcel 已提交
7
因此,今天,我们将使用 Apache ANT 的 junitreport 任务来生成一个任务。 准备点亮!
W
init  
wizardforcel 已提交
8

W
wizardforcel 已提交
9
从我的[上一篇文章](https://javabeginnerstutorial.com/selenium/10a-advanced-webdriver-apache-ant/)中,我们生成了带有“`junit`”作为默认 JUnit 输出目录的构建文件。 用简单的英语来说,这仅意味着将作为 JUnit 报告任务的一部分生成的所有文件都放置在名为“`junit`”的目录下。
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
#### 步骤 1 :
W
init  
wizardforcel 已提交
12

W
wizardforcel 已提交
13
在项目“Selenium”下,将生成名为“`build.xml`”的 ANT 构建文件。 我已经在“`com.blog.junitTests`”包下创建了两个 JUnit 测试用例,分别是 [RadioBtns_Checkboxes.java](https://javabeginnerstutorial.com/selenium/9t-webdriver-handling-radio-buttons-checkboxes/)[SelectItems.java](https://javabeginnerstutorial.com/selenium/9u-webdriver-select-items-two-ways/) ,如下图所示。 该代码可在相应的帖子中找到。 只需单击文件名即可进行导航。
W
init  
wizardforcel 已提交
14 15


W
wizardforcel 已提交
16
<noscript><img alt="Package structure" class="alignnone size-full wp-image-13022" height="273" src="img/bbbccea0a3928f2e18e1c51e8230d1e6.png" width="354"/><h4><strong>步骤 2 </strong></h4><p>打开“build.xml”,并确保自动生成“junitreport”任务。 以下是我的项目的目标(初始化,清理,构建,RadioBtns_Checkboxes,SelectItems 和 junitreport)。</p><pre><code class="language-xml">&lt;project basedir="." default="build" name="Selenium"&gt; &lt;property environment="env"/&gt; &lt;property name="junit.output.dir" value="junit"/&gt; &lt;property name="debuglevel" value="source,lines,vars"/&gt; &lt;property name="target" value="1.8"/&gt; &lt;property name="source" value="1.8"/&gt; &lt;target name="init"&gt; &lt;mkdir dir="bin"/&gt; &lt;copy includeemptydirs="false" todir="bin"&gt; &lt;fileset dir="src"&gt; &lt;exclude name="**/*.java"/&gt; &lt;/fileset&gt; &lt;/copy&gt; &lt;/target&gt; &lt;target name="clean"&gt; &lt;delete dir="bin"/&gt; &lt;/target&gt; &lt;target name="build" depends="init"&gt; &lt;echo message="${ant.project.name}: ${ant.file}"/&gt; &lt;javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}"&gt; &lt;src path="src"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/javac&gt; &lt;/target&gt; &lt;target name="RadioBtns_Checkboxes"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.RadioBtns_Checkboxes" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="SelectItems"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.SelectItems" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="junitreport"&gt; &lt;junitreport todir="${junit.output.dir}"&gt; &lt;fileset dir="${junit.output.dir}"&gt; &lt;include name="TEST-*.xml"/&gt; &lt;/fileset&gt; &lt;report format="frames" todir="${junit.output.dir}"/&gt; &lt;/junitreport&gt; &lt;/target&gt; &lt;/project&gt;</code></pre><h4><strong>步骤 3 </strong></h4><p>可以通过以下任意一种方式执行构建,</p><p>1.右键单击构建文件(build.xml)-&gt;运行方式-&gt; Ant Build</p><p><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/9_RepRunMethod1-1.jpg" height="416" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20775%20416'%3E%3C/svg%3E" width="775"/></p><noscript><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" height="416" src="img/1f57bab2e596907cfafba4a0f756ff86.png" width="775"/><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad ezoic-adl" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span> 2.右键单击构建文件(build.xml)-&gt;运行方式-&gt;外部工具配置-&gt;运行</p><p>3.蚀工具栏中的快捷方式运行图标</p><p><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" data-lazy-sizes="(max-width: 536px) 100vw, 536px" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg" data-lazy-srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" height="201" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20536%20201'%3E%3C/svg%3E" width="536"/></p><noscript><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" height="201" sizes="(max-width: 536px) 100vw, 536px" src="img/6a5b08798471e00aeee6fc71c824d310.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" width="536"/><p>让我们采用第二种方法,看看如何使用“外部工具配置”选项修改现有配置。</p><p>因此,右键单击构建文件-&gt;运行方式-&gt;外部工具配置:这将打开一个弹出窗口。 选择“主要”标签,并确保选择了正确的构建文件。</p><p><img alt="Main Configuration" class="alignnone size-full wp-image-13023" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/7_RepMainConfig-1.jpg" height="248" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20783%20248'%3E%3C/svg%3E" width="783"/></p><noscript><img alt="Main Configuration" class="alignnone size-full wp-image-13023" height="248" src="img/4fa275eb64b4fb422b00608b496fe7f8.png" width="783"/><p>然后点击“目标”标签。</p><ol><li>将列出构建文件中定义的所有目标名称,并且仅检查默认目标。 点击“运行”,选择要执行的所有目标。</li><li>在“目标执行顺序”框中,将从上一步中选择的所有目标按执行顺序列出。</li><li>如果您想更改订单,请点击“订单...”。 这将打开“订单目标”弹出窗口。</li><li>我们可以通过选择目标并相应地单击“上”或“下”按钮来在此弹出窗口中指定目标执行顺序。 点击“确定”以确认最终的执行顺序。</li><li>完成所有操作后,点击“应用”和“运行”</li></ol><p><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/8_RepTargetConfig.jpg" height="591" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20845%20591'%3E%3C/svg%3E" width="845"/></p><noscript><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" height="591" src="img/8af13d9bb45d3942d6d51b53f17f884f.png" width="845"/><h4><strong>步骤 4 </strong></h4><p>可以通过查看控制台视图来验证成功执行。 它显示了按配置顺序执行的所有目标以及基于构建文件中提到的日志记录级别的日志。 如图所示,将显示运行构建所花费的总时间以及消息“BUILD SUCCESSFUL”或“BUILD FAILED”。</p><p><img alt="Console Output" class="alignnone size-full wp-image-13019" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/11_RepConsoleOutput.jpg" height="515" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20790%20515'%3E%3C/svg%3E" width="790"/></p><noscript><img alt="Console Output" class="alignnone size-full wp-image-13019" height="515" src="img/0fb58b99d8612d1dccb306cceb89eba4.png" width="790"/><h4><strong>步骤 5 </strong></h4><p>在 Eclipse 的“Package Explorer”视图中,右键单击该项目,然后单击“Refresh”或 F5。 已经创建了“junit”文件夹(在生成构建文件时在“JUnit 输出目录”框中指定的名称)。 这是 JUnit 报告(index.html)和 XML 一起提供的地方,用于执行的每个测试用例,显示其成功或失败。</p><p><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/12_RepJUnitFolder-1.jpg" height="402" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20646%20402'%3E%3C/svg%3E" width="646"/></p><noscript><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" height="402" src="img/c5e6944f046312e1960f0c4919875688.png" width="646"/><p>或者,在文件浏览器中导航到项目路径,然后双击“junit”文件夹。</p><h4><strong>步骤 6 </strong></h4><p>在浏览器中打开“<strong> index.html </strong>”,并检查生成的默认报告。 这将在左窗格中显示所涉及的包和所有已执行的 Java 文件。</p><p>右窗格显示测试结果的摘要,其中包括测试的总数,失败,错误,跳过,成功率和执行时间。 还显示了包列表,其中包含类似的详细信息。</p><p>要深入查看所有已执行的测试或失败等,请单击显示的数字,如下图所示,</p><p><img alt="Generated report" class="alignnone size-full wp-image-13021" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/13_ReportGenerated.jpg" height="436" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20761%20436'%3E%3C/svg%3E" width="761"/></p><noscript><img alt="Generated report" class="alignnone size-full wp-image-13021" height="436" src="img/f6fcd46485176dabfbaa70e1208db3aa.png" width="761"/><p>用 Apache ANT 生成 JUnit 报告不是一件容易的事吗? 猜猜这就是我们今天要做的。</p><p>在另一篇文章中再见。 祝您报告愉快!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/" title="10c. Advanced WebDriver – JUnit Report Customization"> 10c。 高级 WebDriver – JUnit 报表自定义</a></h5></div></div> </body> </html></noscript>