lakjdsf

来源:互联网 发布:淘宝邀请的活动有用吗 编辑:程序博客网 时间:2024/06/07 01:08

由头:项目要做新版本,我打算把我知道的新知识都用起来,所以,单元测试不能少。


用的是Espresso框架。

在src中有两个包,分别是:test与androidTest 

①    test:是测试不涉及Activity,UI组件的纯Java方法。

直接在电脑上直接测试。

        androidTest:涉及UI,Android组件的都在该路径下测试。

需要连接真机,或者模拟器进行测试。

②    在AS2.2中已经有现成的。在build.grade文件中:有两部分,

testCompile'junit:junit:4.12'----->对应test路径下的测试

 

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    ----->对应androidTest路径下的测试   
③  使用:选中要测试的类方法:Mac上快捷键:command + shift + T,选择:create new test,下一步
选择是在test路径下,还是androidTest路径下创建单元测试,然后OK就可以了。
④  实践:
test下,简单测试:
上源代码,很简单一个求和,一个求乘积
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. public class Caculation {  
  2.   
  3.     public double sum(double numA,double numB){  
  4.         return numA + numB;  
  5.     }  
  6.   
  7.     public double multiply(double numA,double numB){  
  8.         return numA * numB;  
  9.     }  
  10.   
  11. }  
按照③步骤,调用方法用到了对象,所以创建一个对象。用到了注解的方式,所以为了方便单元测试,功能为了更好的模块下,方便解耦,我们还要学习dagger2 匕首2的使用姿势.本章的题外话。
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Created by liuhaoqing on 17/3/16. 
  3.  */  
  4. public class CaculationTest {  
  5.     private Caculation mCaculation;  
  6.   
  7.     @Before       //调用测试方法之前执行  
  8.     public void setUP() throws Exception{  
  9.         mCaculation = new Caculation();  
  10.     }  
  11.     @Test  
  12.     public void testSum() throws Exception{  
  13.         assertEquals(2,mCaculation.sum(1,1),0);//断言 :<span style="font-family: Menlo; font-size: 9pt;">assertEquals</span>  
  14.   
  15.     }  
  16.   
  17.     @Test  
  18.     public void testMultiply()throws Exception{  
  19.         assertEquals(2,mCaculation.multiply(2,1),0);  
  20.     }  
  21.   
  22.   
  23. }  
=====================华丽分割线=============================
androidTest下,简单测试
[html] view plain copy
在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="mobile.bank.ecitic.com.testdemo.MainActivity">  
  12.   
  13.     <TextView  
  14.         android:id="@+id/textView"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="hello world!"/>  
  18.   
  19.     <EditText  
  20.         android:id="@+id/editText"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_below="@+id/textView"  
  24.         android:hint="Enter your name here"/>  
  25.   
  26.     <Button  
  27.         android:id="@+id/sayHello"  
  28.         android:layout_width="match_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_below="@+id/editText"  
  31.         android:text="Say hello!"/>  
  32. </RelativeLayout>  
效果图:
Java代码:
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package mobile.bank.ecitic.com.testdemo;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     private TextView textView;  
  13.     private EditText editText;  
  14.     private Button button;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         initUI();  
  21.     }  
  22.   
  23.     private void initUI(){  
  24.         textView = (TextView) findViewById(R.id.textView);  
  25.         editText = (EditText) findViewById(R.id.editText);  
  26.         button = (Button) findViewById(R.id.sayHello);  
  27.         button.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 textView.setText("Hello:"+editText.getText().toString() + "!");  
  31.             }  
  32.         });  
  33.     }  
  34. }  
涉及UI的单元测试代码:  有一点需要注意,在使用onView等方法时候,AS没有提示。我找了半天以为引入的包不对,我反复比对,没错。最后我是点击报错提示,引入进来的。
此处有点小坑。学习摸索前进,不能避免的。具体注释使用,见代码注释。
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package mobile.bank.ecitic.com.testdemo;  
  2.   
  3. import android.support.test.rule.ActivityTestRule;  
  4. import android.support.test.runner.AndroidJUnit4;  
  5.   
  6. import org.junit.Rule;  
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9.   
  10. import static android.support.test.espresso.Espresso.onView;  
  11. import static android.support.test.espresso.action.ViewActions.click;  
  12. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;  
  13. import static android.support.test.espresso.action.ViewActions.typeText;  
  14. import static android.support.test.espresso.assertion.ViewAssertions.matches;  
  15. import static android.support.test.espresso.matcher.ViewMatchers.withId;  
  16. import static android.support.test.espresso.matcher.ViewMatchers.withText;  
  17. import static org.junit.Assert.*;  
  18.   
  19. /** 
  20.  * Created by liuhaoqing on 17/3/16. 
  21.  */  
  22. @RunWith(AndroidJUnit4.class)  
  23. public class MainActivityTest {  
  24.     private static final String STRING_TO_BE_TYPED = "LHQ";  
  25.   
  26.     @Rule  
  27.     public ActivityTestRule<MainActivity> mainActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class);  
  28.   
  29.     @Test  
  30.     public void sayHello() throws Exception{  
  31.         //提供onView()来注入组件,通过withId 找到组件,写入STRING_TO_BE_TYPED所指向字符串,并关闭软键盘  
  32.         onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED),closeSoftKeyboard());  
  33.         //点击id为sayHello的button  
  34.         onView(withId(R.id.sayHello)).perform(click());  
  35.         onView(withId(R.id.textView)).check(matches(withText("Hello:"+STRING_TO_BE_TYPED + "!")));  
  36.     }  
  37. }  
运行测试文件,run的时候,会自动执行代码,执行完,红色条表示有错误。绿色条表示成功。
参考文章:
感谢:http://www.jianshu.com/p/90095c989311  AndroidStudio测试(二)UI测试
http://www.jianshu.com/p/587994b4727c        (三)为什么要用单元测试 总结
        http://blog.csdn.net/xuguoli_beyondboy/article/details/50475728    Android Espresso单元测试
http://blog.csdn.net/javaandroid730/article/details/53327276  Android中如何简单的做单元测试   讲解常用断言方法 

0 0
原创粉丝点击