java junit框架的windows自动化-自动运行junit程序简述

  在京东混了一个月,基本有点稳定了,觉得也有所余力了现在,继续写博客吧,不过以后更新也许不是那么频繁了
  本人使用的是junit框架,对开发是一个单元测试的java框架,但是对测试而言是java的基石之一,与testng差不多平分秋色(好吧,其实是稍微差一点)
在上文http://www.cnblogs.com/xuezhezlr/p/7773614.html简单介绍了junit框架后,这里作一个简单的比较
  junit框架与传统main函数的主要区别:
1junit框架其实质包含多个main函数,相互间做到了一定程度上的互不影响,即便一个抱错也不会对所有用例有所影响,对于针对一个测试类编写大量的测试用例,是很相近的,更贴近测试
2junit框架有分明的before,after,beforeclass等相关标注,一定程度上简化了代码编写,不过main函数比较复杂的for循环也可以做到
3ant在执行junit框架的时候有对应的标签,可以生成一定的报告来进行呈现
4junit其本身有大量的测试人员使用,使用该框架可以直接降低团队沟通成本

  其实最主要的问题是,笔者只是对junit框架有一点粗浅的了解,,,其实对测试而言,特别是自动化测试,不一定需要你对testng,python等东西都有了解,只需要能自己用自己的办法写出来自己觉得合适,别人也觉得ok的东西就好,达到目的的重要性远大于手段的华丽
  下面开始演示一个junit框架的代码自动执行,发邮件的操作

  首先先在上文http://www.cnblogs.com/xuezhezlr/p/7725301.html要求的环境中新建立一个java的工程,一般的java的包是有目录层次的,本文也模拟一般的环境下多级目录的情景,在src中的目录依次建立文件夹,在其中建立一个方法testzhiyinlou.java,笔者的是E:\work\TestAuto_Integration\src\main\java\JD\jincai\ppt\testzhiyinlou.java,各位的要自行摸索,,,
  在java文件中写出下面代码
package JD.jincai.ppt;

import org.junit.Test;
import org.junit.Before;
public class testzhiyinlou {
int x=0;
@Before
public void setUp() throws Exception {
x++;
}
@Test
public void testszlr1() throws Exception {
System.out.println(++x);
}
@Test
public void testszlr2() throws Exception {
System.out.println(++x);
}
}

  理论上是可以运行的,,,不能运行自行摸索,结果应该是两个2
  也就是说在我们已经学会了如何自动运行.bat文件的基础上,本文主要是讲述如何用一个.bat文件运行上述代码这么一件事情
  说直白点,我们要做的是,自动运行.bat文件然后.bat文件中会调用ant命令,而ant命令呢,又直接调用build.xml文件,所以,所谓junit自动化和java自动化的区别,本质是代码和build.xml文件的区别~
  代码已经贴上去了,我们说一下与上文配套的build文件
  建立一个build.xml文件,写上如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="junit and report" name="TestAuto">
<tstamp prefix="mydate">
<format property="day" pattern="yyyy-MM-dd"/>
<format property="time" pattern="HH:mm:ss"/>
<format property="dt" pattern="yyyy-MM-dd HH:mm:ss.SSS"/>
<format property="dt1" pattern="yyyyMMdd" offset="-1" unit="day" />
<format property="dt2" pattern="yyyyMMdd" offset="-3" unit="day" />
<format property="dt3" pattern="yyyyMMdd" offset="0" unit="day" />
<format property="dt4" pattern="yyyyMMddHHmmss" />
</tstamp>

<property name="appname" value="TestAuto"/>
<property name="build.dir" value="C:\Users\zouleiran\Desktop\autoTest" />
<property name="build.class.dir" value="E:\work\TestAuto_Integration\target\classes" />
<property name="build.lib.dir" value="${build.dir}\lib" />
<property name="build.report.dir" value="${build.dir}\report\${appname}_zlr" />
<property name="build.report.name" value="${appname}_zlr" />


<target name="junit and report" depends=""
description="run test and send report">
<delete dir="${build.report.dir}" />
<mkdir dir="${build.report.dir}" />

<junit printsummary="on" fork="true" showoutput="true">
<sysproperty key="file.encoding" value="GBK" />
<formatter type="xml" usefile="true" />
<classpath>
<fileset dir="${build.lib.dir}" includes="**\*.jar" />
<pathelement path="${build.class.dir}" />
</classpath>
<batchtest todir="${build.report.dir}">
<formatter type="brief" usefile="true"/>
<fileset dir="${build.class.dir}">
<include name="JD\jincai\ppt\testzhiyinlou.class"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${build.report.dir}">
<fileset dir="${build.report.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${build.report.dir}" />
</junitreport>
</target>

</project>

下面进行讲解
  先说明白,E:\work\TestAuto_Integration\src\main\java\JD\jincai\ppt\testzhiyinlou.java是我的机器的java文件路径,而class文件路径E:\work\TestAuto_Integration\target\classes\JD\jincai\ppt\testzhiyinlou.class
  在build文件中,$这个美元符号是参数化的意思,下面对文件的参数化进行大概讲解
build.dir是指我们的文件的位置,一般是报告和其他的文件的相近路径,后文没有直接使用
build.class.dir是class文件的位置,在文中主要是
<fileset dir="${build.class.dir}">
<include name="JD\jincai\ppt\testzhiyinlou.class"/>
</fileset>
  这两句使用了,也就是说,build.class.dir=class文件路径 减去 package JD.jincai.ppt这个class的相对路径,即:E:\work\TestAuto_Integration\target\classes\JD\jincai\ppt\testzhiyinlou.class-JD\jincai\ppt\testzhiyinlou.class=E:\work\TestAuto_Integration\target\classes
  本质是指定运行时候的class方法的起点位置,理应在class文件的根目录,然后利用这里的include name这个参数,指选哪个class即可运行哪一个class,当需要运行多个class时候。可以用正则,*等表示也可以用多行表示,如下
<include name="JD\jincai\ppt\testzhiyinlou1.class"/>
<include name="JD\jincai\ppt\testzhiyinlou2.class"/>
<include name="JD\jincai\ppt\testzhiyinlou3.class"/>

  build.lib.dir这个参数是运行代码时候所依赖的jar包所在位置(本文不讨论mvn依赖,只讨论最原始的junit框架运行),由于ant本身有一定的jar包所以一般执行ant的lib目录,再把项目所需要的jar包放在lib下即可执行,由于事例过于简单,无需额外jar包故无需添加
  xml中classpath标签是指我们的jar包所在地点,而batchtest是指我们真正运行的@Test的文件路径
  junitreport是junit报告阶段,使用的参数时build.report.name和build.report.dir,这两个参数直接指定了报告文件夹所在位置
  以上便是我们的程序,其实在xml中的区别就是,ant针对junit框架有专门的标签,可以直接使用junit标签来做,同时也有junitreport来直接生成报告,下一篇随笔讲发报告的问题

posted @ 2018-01-06 18:09  学者zlr  阅读(1178)  评论(2编辑  收藏  举报