Android单元测试

qnng8004 8年前
   <h2>Android单元测试</h2>    <p>创建android studio工程,在app目录下的build.gradle中加入JUnit4的依赖。</p>    <pre>  <code class="language-java">dependencies {      compile fileTree(dir: 'libs', include: ['*.jar'])      testCompile 'junit:junit:4.12'      compile 'com.android.support:appcompat-v7:24.0.0-beta1'  }</code></pre>    <h3><strong>创建Unit Test</strong></h3>    <p>创建测试类Tools</p>    <pre>  <code class="language-java">public class Tools {      public double sum(double a ,double b){          return 0;      }        public double abs(double value){          return 0;      }  }</code></pre>    <p>在待测试的类(Tools)代码编辑器内,右键选择Go to -> Test</p>    <p>或者使用快捷键Ctrl + Shift + T,Create New Test 。</p>    <p>Testing library选择JUnit4</p>    <p>Generate选择setUp/@Before</p>    <p>Destionation package选择生成的测试类的包路径</p>    <p>如下图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/9d307920d709253e1c880fe37923eaeb.png"></p>    <p style="text-align:center">1.PNG</p>    <p>创建成功后会在app/src/test/java/me/peace/junit下生成测试类(me/peace/junit即Destionation package)</p>    <p>一般与javas相关的单元测试选择test/java目录,与android相关的选择androidTest/java,若缺少这两个目录请自行创建</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ac6619b53fcd4a47c1e12f258bb49e1a.png"></p>    <p style="text-align:center">2.PNG</p>    <p>在这个测试类中加入具体的测试内容</p>    <pre>  <code class="language-java">public class ToolsTest {      private Tools mTools;        @Before      public void setUp() throws Exception {          mTools = new Tools();          System.out.println("Before - setUp");      }        @Test      public void testSum() throws Exception {          assertEquals(48d,mTools.sum(46d,2d),0);          System.out.println("Test - testSum");      }        @Test      public void testAbs() throws Exception {          assertEquals(46,mTools.abs(-46),0);          System.out.println("Test - testAbs");      }  }</code></pre>    <h3><strong>实现Tools类</strong></h3>    <pre>  <code class="language-java">public class Tools {      public double sum(double a ,double b){          return a + b;      }        public double abs(double value){          return Math.abs(value);      }  }</code></pre>    <h3><strong>Run Test Unit</strong></h3>    <p>右键ToolsTest类,选择Run->ToolsTest</p>    <p>测试结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/eb2a355cebf6303198c3c6579e45f471.png"></p>    <p style="text-align:center">3.PNG</p>    <h3><strong>补充</strong></h3>    <p>Generate选择setUp/@Before和tearDown/@After</p>    <p>其运行顺序是setUp->测试的方法->tearDown(即Before->Test->After,且每个Test方法都会去执行一次setUp和tearDown方法)</p>    <p>测试结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/08175106bb1ff7034565e11350f701f6.png"></p>    <p style="text-align:center">4.PNG</p>    <h3><strong>使用Espresso进行Instrumentaion Test</strong></h3>    <p>在build.gradle中配置Espresso,在defaultConfig中添加</p>    <pre>  <code class="language-java">testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"</code></pre>    <p>在dependencies中添加</p>    <pre>  <code class="language-java">androidTestCompile 'com.android.support.test:runner:0.5'  androidTestCompile 'com.android.support.test:rules:0.5'  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'</code></pre>    <p>完整的内容</p>    <pre>  <code class="language-java">apply plugin: 'com.android.application'    android {      compileSdkVersion 24      buildToolsVersion "24.0.2"        defaultConfig {          applicationId "me.peace.espressostudy"          minSdkVersion 15          targetSdkVersion 24          versionCode 1          versionName "1.0"          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"      }      buildTypes {          release {              minifyEnabled false              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'          }      }  }    dependencies {      compile fileTree(dir: 'libs', include: ['*.jar'])      testCompile 'junit:junit:4.12'      compile 'com.android.support:appcompat-v7:24.0.0-beta1'      androidTestCompile 'com.android.support:support-annotations:24.0.0-beta1'      androidTestCompile 'com.android.support.test:runner:0.5'      androidTestCompile 'com.android.support.test:rules:0.5'      androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'  }</code></pre>    <p>编辑layout的布局文件,效果如图</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/2a0e8d84f8c16bb6af5719d41426dea4.png"></p>    <p style="text-align:center">5.PNG</p>    <p>在MainActivity中加入如下代码</p>    <pre>  <code class="language-java">//按钮点击事件处理   public void print(View v){       TextView textView = (TextView)findViewById(R.id.textview);       EditText editText = (EditText)findViewById(R.id.edittext);       textView.setText("message => " + editText.getText().toString().trim());   }</code></pre>    <p>在androidTest/java目录下创建MainActivityTest并且添加如下代码</p>    <pre>  <code class="language-java">import static android.support.test.espresso.Espresso.onView;  import static android.support.test.espresso.action.ViewActions.click;  import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;  import static android.support.test.espresso.action.ViewActions.typeText;  import static android.support.test.espresso.assertion.ViewAssertions.matches;  import static android.support.test.espresso.matcher.ViewMatchers.withId;  import static android.support.test.espresso.matcher.ViewMatchers.withText;    @RunWith(AndroidJUnit4.class)  @LargeTest  public class MainActivityTest{      private String mExpectedString = "Hello";        @Rule      public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);        @Test      public void print(){          onView(withId(R.id.edittext)).perform(typeText(mExpectedString),closeSoftKeyboard());          onView(withText(R.string.print)).perform(click());          onView(withId(R.id.textview)).check(matches(withText("message => " + mExpectedString)));      }  }</code></pre>    <p>选择MainActivityTest,右键Run 'MainActivityTest'</p>    <p>手机屏幕就会看到被执行的动作,最后会看到as上输出测试结果。</p>    <p><img src="https://simg.open-open.com/show/3c0b47a5800f6e55fe992d8a1d09fd44.png"></p>    <p> </p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/e8f4db41579d</p>    <p> </p>