ant接口自动化 junit_ant整合junit自动化测试

一. 使用Junit进行测试

1. Java业务代码:

public class HelloWorld {

// 测试返回"world"

public String hello() {

return "world";

}

// 测试返回"hello"

public String world() {

return "hello";

}

// 测试为空

public String nil() {

return null;

}

// 测试不为空

public String notNil() {

return "abc";

}

// 测试抛出异常

public String ext() {

return null;

}

}

2. 使用junit3测试, 继承TestCase

import junit.framework.TestCase;

public class HelloWorldTest extends TestCase {

private HelloWorld helloWorld;

@Override

protected void setUp() throws Exception {

helloWorld = new HelloWorld();

System.out.println("helloWorld init");

}

public void testHello() {

String str = helloWorld.hello();

assertEquals("测试world失败", str, "world");

}

public void testWorld() {

String str = helloWorld.world();

assertEquals("测试world失败", str, "hello");

}

public void testNotNil() {

assertNotNull("对象为空", helloWorld.notNil());

}

public void testNil() {

assertNull("对象不为空", helloWorld.nil());

}

public void testExt() {

try {

helloWorld.ext();

fail("没有抛出异常");

} catch (NumberFormatException e) {

}

}

@Override

protected void tearDown() throws Exception {

System.out.println("hello world destory");

helloWorld = null;

}

}

3. 使用junit4测试, 使用注解

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import static org.junit.Assert.*; // 静态引入, assertEquals兼容junit3

public class HelloWorldTest {

private HelloWorld helloWorld;

@Before

public void setUp() {

helloWorld = new HelloWorld();

}

@Test

public void testHello() {

String str = helloWorld.hello();

assertEquals("hello测试失败",str,"world");

}

@Test

public void testWorld() {

String str = helloWorld.world();

assertEquals("world测试失败",str, "hello");

}

@Test

public void testNil() {

assertNull("对象不为空",helloWorld.nil());

}

@Test

public void testNotNil() {

assertNotNull("对象为空", helloWorld.notNil());

}

@Test(expected=NumberFormatException.class)

public void testExt() {

helloWorld.ext();

}

@After

public void tearDown() {

helloWorld = null;

}

}

二. ant整合junit测试

1. 生成测试报告:

1.开始清理文件

2.项目的初始化

3.编译源文件

classpathref="lib_path" includeantruntime="true"/>

4.编译测试文件

classpathref="compile_path" includeantruntime="true"/>

5.运行单元测试

6.测试结束

2. 生成doc文档并打包

1.开始清理文件

2.项目的初始化

3.生成doc文档

use="true" packagenames="com.zdp.*" destdir="${build.doc}"

charset="UTF-8" docencoding="UTF-8" encoding="UTF-8">

4.生成zip包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值