参考文章:http://www.jianshu.com/p/03118c11c199 推荐android测试相关:http://www.jianshu.com/collection/b26df74781cb
使用AndroidStudio进行单元测试时,与当前网上众多资料进行比较。在较新版本下(当前我使用的是2.0)的studio中不需要自己进行BuildVariants的设置以及依赖JUnit包。studio会自动完成这些操作。
进行基本的单元测试的操作流程如下 图中是我当前的目录结构,Calculator是实现计算的实体类,首先在Calculator中进行基本的逻辑编写,提供加减乘除四个方法。studio提供快速创建Test类的方法,对类名右击选择goto->test可以快速的创建或跳转到对应类。在Create Test中提供自动生成的选项,分别是对方法的测试与测试前后进行的一些行为。
在CalculatorTest文件中,在@Before注解中进行实体对象的创建,在@Test中分别进行方法的测试,代码如下:
public class CalculatorTest { private Calculator mCal; @Before public void setUp() throws Exception { mCal = new Calculator(); } @Test public void testSum() throws Exception { assertEquals(6d, mCal.sum(1d, 5d),0); } @Test public void testSubstract() throws Exception { assertEquals(2d, mCal.substract(7d, 5d),0); } @Test public void testMultiply() throws Exception { assertEquals(35d, mCal.multiply(7d, 5d),0); } @Test public void testDivide() throws Exception { assertEquals(2d, mCal.divide(10d, 5d),0); } }编写结束后运行即可得到测试结果。 assertEquals的三个参数分别为预期值,实际值,误差范围 将Calculator中sum方法返回值改为a-b,当测试结果与预期不一致时,显示结果如下:描述了预期值与实际值的结果与出错行数。
androidTest文件夹是Android Instrumentation Tests的文件夹 test文件夹是Unit Tests的文件夹
首先进行gradle的配置 在defaultConfig下添加 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 在android下添加
packagingOptions { exclude 'LICENSE.txt' }在dependencies下添加
androidTestCompile 'com.android.support.test:runner:0.2' androidTestCompile 'com.android.support.test:rules:0.2' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'当与v7包版本冲突时,将v7包的版本改为与test相匹配即可。
在MainActivity中进行相关逻辑处理与代码编写。 实现通过点击按钮,TextView显示EditText中的内容。 再创建相关测试类MainActivityInstrumentationTest,在代码中确定测试的类,以及测试的内容。通过 onView(withId())或onView(withText())获取相应控件并进行模拟操作。相关代码如下:
@RunWith(AndroidJUnit4.class) @LargeTest public class MainActivityInstrumentationTest { private static final String STRING_TO_BE_TYPED = "androidTest"; @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>( MainActivity.class); @Test public void sayHello() { //获取editText并输入字符串 onView(withId(R.id.et)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1 //获取button并模拟点击 // onView(withText("Say hello!")).perform(click()); //line 2 onView(withId(R.id.btn)).perform(click()); //比较应用中textView与期望字符自否匹配 String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!"; onView(withId(R.id.tv)).check(matches(withText(expectedText))); //line 3 } }MainActivity中的逻辑:
public class MainActivity extends Activity { private TextView tv; private Button btn; private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = et.getText().toString(); tv.setText("Hello, "+name+"!"); } }); } private void findViews() { tv = (TextView) findViewById(R.id.tv); btn = (Button) findViewById(R.id.btn); et = (EditText) findViewById(R.id.et); } }运行测试类时,在模拟器上可以看到进行了@Test中的具体步骤:
通过onView可以获取控件,当需要点击的选项为ListView或Spinner这类控件中没有id的选项时,可以通过onData对其进行获取。 写出一个界面,里面有一个Spinner和TextView,通过点击Spinner的选项进行TextView显示的控制。测试的流程为,先点击Spinner,通过onData获取Spinner中的某个条目并进行模拟点击,最后验证TextView显示的字符串是否为Spinner的选项。关于onData中的参数,通过获取所有类型为String的选项并进行过滤。
package com.example.administrator.testspace; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; 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; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsInstanceOf.instanceOf; /************************************** * Created by chenzilong on 2016/8/17.* *************************************/ @RunWith(AndroidJUnit4.class) @LargeTest public class SpinnerActivityTest { @Rule public ActivityTestRule<SpinnerActivity> mActivityRule = new ActivityTestRule<>( SpinnerActivity.class); @Test public void getItem(){ onView(withId(R.id.spinner)).perform(click()); onData(allOf(is(instanceOf(String.class)), is("two"))).perform(click()); onView(withId(R.id.tv)).check(matches(withText("two"))); } }测试结果:
通过onView和onData的操作流程如图:
因此可以获取空间中条目位置进行模拟点击,之前的代码可以改为:
@Test public void getItem(){ onView(withId(R.id.spinner)).perform(click()); onData(allOf(is(instanceOf(String.class)))).atPosition(3).perform(click()); onView(withId(R.id.tv)).check(matches(withText("four"))); }