一直在写代码,每次都是把功能写完了再去测试,当出现bug,很多地方都需要改动很麻烦,也浪费时间,开发效率低下。下面就来整理一下Android APP开发过程中:测试方法——单元测试(Junit)
测试的相关概念:
1.测试是否知道源代码
黑盒测试 不知道代码
白盒测试 知道代码
2.按照测试的粒度
方法测试
单元测试 Junit
集成测试
系统测试
3.按照测试的暴力程度
冒烟测试 硬件
压力测试 12306
monkey测试: adb shell下的一个测试指令。 adb shell + monkey -p packagename count;
Eclipse 单元测试:
1.创建一个类Test继承AndroidTestCase,那么该类就具备单元测试的功能。
2.需要在androidmanifest.xml中的application节点下配置一个uses-library;
<uses-library android:name="android.test.runner" />
3.需要在androidmanifest.xml中的manifest节点下配置一个instrumentation;targetPackage:需要测试的工程的包名。
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.junit" />
配置好androidmanifest以后,在Test类中去写出测试的逻辑代码就ok了。
Android Studio 单元测试:
1、创建一个单元测试的类
package yan.chen.com.everydayrecommand;
/**
* Created by chen on 2017/12/7.
*/
public class JunitTest {
public int addMath(int x,int y){
return x+y;
}
}
![](https://img.haomeiwen.com/i2406435/734c7275d98a096b.png)
![](https://img.haomeiwen.com/i2406435/92f53acb8c3246a4.png)
选择Creat New Test...
![](https://img.haomeiwen.com/i2406435/14061afdb1149c66.png)
点击OK
![](https://img.haomeiwen.com/i2406435/da7a70e80fec7f15.png)
继续点击OK,就生成下面这个测试类
package yan.chen.com.everydayrecommand;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by chen on 2017/12/7.
*/
public class JunitTestTest {
@Test
public void addMath() throws Exception {
}
}
在这个测试类中的 addMath()方法里面编写,需要测试的逻辑内容
package yan.chen.com.everydayrecommand;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by chen on 2017/12/7.
*/
public class JunitTestTest {
@Test
public void addMath() throws Exception {
JunitTest junitTest = new JunitTest();
int i = junitTest.addMath(4, 5);
//这里是比较的期望值(前者)和实际值(后者)
assertEquals(9,i);
}
}
![](https://img.haomeiwen.com/i2406435/d7ac2784b4e4e0dc.png)
运行之后的结果如下:
![](https://img.haomeiwen.com/i2406435/5a3f0e90947c247b.png)
单元测试完成了,当然单元测试失败,会提示你期望值与实际值是不同的,自己去找找原因。
网友评论