Android -- junit测试框架,logcat获取log信息

1. 相关概念

白盒测试:
知道程序源代码.

 根据测试的粒度分为不同的类型
  方法测试 function test
        单元测试 unit test        
        集成测试 intergation test
 根据测试的次数分为不同的类型
        冒烟测试 smoke test (adb shell monkey 测试点击N次)
        压力测试 pressure test

黑盒测试:
8k
不知道程序的源代码,主要操作应用程序的业务逻辑.

logcat
System.out.println();

日志信息等级
verbose  提示
debug    调试
info   
warn     警告
error    错误

2. 示例代码

CalcService.java, 需要测试的service类

public class CalcService {
	private static final String tag = "CalcService";
	/**
	 * 相加的业务方法
	 * @param x 第一个int
	 * @param y 第二个int
	 * @return
	 */
	public int add(int x ,int y){
		Log.v(tag, "x="+x);
		Log.d(tag, "y="+y);
		int result = x+y;
		Log.i(tag,"result="+result);
		Log.w(tag,"result="+result);
		Log.e(tag,"result="+result);
		
		System.out.println("out result="+result);
		System.err.println("error result="+result);
		
		return result;
	}
}

TestCalcService.java 测试类,继承androidTestCase

public class TestCalcService extends AndroidTestCase {

	/**
	 * 所有的测试方法  应该把异常抛给测试框架
	 * @throws Exception
	 */
	public void testAdd() throws Exception{
		CalcService calcService = new CalcService();
		int result = calcService.add(3, 5);
		assertEquals(8, result);
	}
}

AndroidManifest.xml,要使用junit需要配置uses-library 和 instrumentation

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.junit"
    android:versionCode="1"
    android:versionName="1.0" >

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:label="Tests for My App"
    android:targetPackage="com.itheima.junit" />
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner" />
        <activity
            android:name="com.itheima.junit.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
powermock-module-junit4-common是PowerMock框架中的一个模块,用于与JUnit4集成。这个模块可以在测试过程中模拟和修改静态方法、私有方法、构造函数和final类等。当我们需要对这些难以测试的代码进行单元测试时,可以使用这个模块来解决这个问题。 要下载powermock-module-junit4-common,需要先确保在项目的构建工具中引入了PowerMock框架的依赖。如果使用Maven进行构建,可以在pom.xml文件中添加以下依赖: <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-common</artifactId> <version>版本号</version> <scope>test</scope> </dependency> 在Gradle中,可以在build.gradle文件的dependencies部分添加以下代码: testImplementation 'org.powermock:powermock-module-junit4-common:版本号' 在配置好依赖之后,再执行构建工具的相关命令,即可自动下载并引入powermock-module-junit4-common模块。 通过使用powermock-module-junit4-common,我们可以更方便地编写单元测试,尤其适用于需要模拟静态方法、私有方法或者处理final类的情况。它为我们提供了更多的测试可能性,使得我们能够更全面地覆盖代码并提高测试覆盖率。同时,它也提供了一系列好用的工具类和注解,让测试代码编写更加简洁高效。 总之,下载并使用powermock-module-junit4-common模块可以为我们提供更强大的测试能力,帮助我们更好地进行单元测试

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值