Android JUnit单元测试周期,异常,测试套件

上篇我们已经讲到怎么在android工程中使用JUnit测试,http://blog.csdn.net/chaoyue0071/article/details/45223115

接下来我们测试案例的周期,异常,当有多个测试文件时怎么用测试套件TestSuit:

我们所写的每一个test开头的方法就是一个测试用例,每一个测试用例都是单独存在的。每一个测试用例运行之前都会调用setUp方法,当一个测试用例执行之后会调用tearDown方法。

package com.wangjialin.junit.lifecycle;

import junit.framework.Assert;
import junit.runner.*;
import android.test.AndroidTestCase;
import android.util.Log;

import com.wangjialin.junit.service.Calculator;

/**
 * 
 *
 * junit 3.8
 * 
	 * 1单元测试方法需要声明为public类型
	 * 2单元测试方法的返回值类型为void
	 * 3按照JUnit3的规范要求单元测试方法的方法命名需要以test开头
	 * 4单元测试方法的参数必须为空
	 * 5单元测试方法最好声明向单元测试框架抛出异常
 */
public class CalculatorTest extends AndroidTestCase {
	private Calculator calculator;
	private static final String TAG = "CalculatorTest";
	
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		calculator = new Calculator();
		Log.i(TAG, "单元测试用例生命周期的初始化方法被调用");
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		Log.i(TAG, "单元测试用例生命周期中的结束方法被调用");
	}

	public void testAdd() throws Throwable
	{
//		calculator = new Calculator();
		
		Log.i(TAG, "testAdd has been invoked!!!");
		
		int result = calculator.add(1, 2);
		
		Assert.assertEquals(3, result);
		
	}
	
	public void testSubtract() throws Throwable
	{
//		calculator = new Calculator();
		
		Log.i(TAG, "testSubtract has been invoked!!!");
		
		int result = calculator.subtract(1, 2);
		
		Assert.assertEquals(-1, result);
	}
	
	public void testMultiply() throws Throwable
	{
//		calculator = new Calculator();
		
		Log.i(TAG, "testMultiply has been invoked!!!");
		
		int result = calculator.multiply(2, 3);
		
		Assert.assertEquals(6, result);
	}
	
	public void testDivide() throws Throwable
	{
//		calculator = new Calculator();
		
		Log.i(TAG, "testDivide has been invoked!!!");
		
		int result = calculator.divide(6, 2);
		
		Assert.assertEquals(3, result);
	}
	
	public void testDivideDivideByZero()
	{
//		calculator = new Calculator();
		
		Log.i(TAG, "testDivideDivideByZero has been invoked!!!");
		
		
		
		Throwable exception = null;
		
		try
		{
			calculator.divide(6, 0);
			
			Assert.fail("测试失败");
		}
		catch(Exception ex)
		{
			exception = ex;
		}
		
		Assert.assertEquals(Exception.class, exception.getClass());
		Assert.assertEquals("除数不能为0", exception.getMessage());
			
	}
}

通过log输出可以看出每个用例都会调用setUp方法和tearDown方法,每个方法也都抛出异常

如果方法不抛出异常,可以加try catch语句。

接下来是自动化测试TestSuit

package com.wangjialin.junit.testsuite;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


/**
 * 
 *
 * 测试类必须要继承于TestCase父类
 */
public class TestAll extends TestCase {
	
	/**
	 * 
	 * 	1自动化测试方法必须声明为public
	 * 	2自动化测试方法必须声明为static类型的
	 * 	3自动化测试方法必须返回Test接口类型的对象
	 * 	4自动化测试方法的名称必须为suite
	 * @return
	 */
	public static Test suite()
	{
		
		TestSuite suite = new TestSuite();
		
		
		suite.addTestSuite(CalculatorTest.class);
		suite.addTestSuite(CalculatorAnotherTest.class);
		
		
		return suite;
	}
}

CalculatorTest.class和CalculatorAnotherTest.class都是需要测试文件。再选中TestAll文件右键运行即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值