https://github.com/android/testing-samples
JUnit 单元测试和AndroidJUnitRunner UI测试
AndroidJunitRunnerSample 效果:
JUnit4 单元测试
**
* JUnit4 unit tests for the calculator logic.
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class CalculatorTest {
private Calculator mCalculator;
@Before
public void setUp() {
mCalculator = new Calculator();
}
@Test
public void addTwoNumbers() {
double resultAdd = mCalculator.add(1d, 1d);
assertThat(resultAdd, is(equalTo(2d)));
}
@Test
public void subTwoNumbers() {
double resultSub = mCalculator.sub(1d, 1d);
assertThat(resultSub, is(equalTo(0d)));
}
@Test
public void subWorksWithNegativeResult() {
double resultSub = mCalculator.sub(1d, 17d);
assertThat(resultSub, is(equalTo(-16d)));
}
@Test
public void divTwoNumbers() {
double resultDiv = mCalculator.div(32d,2d);
assertThat(resultDiv, is(equalTo(16d)));
}
@Test(expected = IllegalArgumentException.class)
public void divDivideByZeroThrows() {
mCalculator.div(32d,0d);
}
@Test
public void mulTwoNumbers() {
double resultMul = mCalculator.mul(32d, 2d);
assertThat(resultMul, is(equalTo(64d)));
}
}
JUnit4 参数化测试
@RunWith(Parameterized.class)
@SmallTest
public class CalculatorAddParameterizedTest {
/**
* @return {@link Iterable} that contains the values that should be passed to the constructor.
* In this example we are going to use three parameters: operand one, operand two and the
* expected result.
*/
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{0, 0, 0},
{0, -1, -1},
{2, 2, 4},
{8, 8, 16},
{16, 16, 32},
{32, 0, 32},
{64, 64, 128}});
}
private final double mOperandOne;
private final double mOperandTwo;
private final double mExpectedResult;
private Calculator mCalculator;
/**
* Constructor that takes in the values specified in
* {@link CalculatorAddParameterizedTest#data()}. The values need to be saved to fields in order
* to reuse them in your tests.
*/
public CalculatorAddParameterizedTest(double operandOne, double operandTwo,
double expectedResult) {
mOperandOne = operandOne;
mOperandTwo = operandTwo;
mExpectedResult = expectedResult;
}
@Before
public void setUp() {
mCalculator = new Calculator();
}
@Test
public void testAdd_TwoNumbers() {
double resultAdd = mCalculator.add(mOperandOne, mOperandTwo);
assertThat(resultAdd, is(equalTo(mExpectedResult)));
}
}
JUnit Suite
package com.example.android.testing.androidjunitrunnersample.suite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Test suite that runs all tests, unit + instrumentation tests.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({UnitTestSuite.class, InstrumentationTestSuite.class})
public class AndroidTestSuite {}
AndroidJUnitRunner ui测试
/**
* JUnit4 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}.
* This class uses the JUnit4 syntax for tests.
* <p>
* With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test
* suite. The {@link AndroidRunnerBuilder} which extends JUnit's
* {@link AllDefaultPossibilitiesBuilder} will create a single {@link
* TestSuite} from all tests and run them.
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class CalculatorInstrumentationTest {
/**
* Use {@link ActivityScenario} to create and launch of the activity.
*/
@Before
public void launchActivity() {
ActivityScenario.launch(CalculatorActivity.class);
}
@Test
public void noOperandShowsComputationError() {
final String expectedResult = getApplicationContext().getString(R.string.computationError);
onView(withId(R.id.operation_add_btn)).perform(click());
onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult)));
}
@Test
public void typeOperandsAndPerformAddOperation() {
performOperation(R.id.operation_add_btn, "16.0", "16.0", "32.0");
}
@Test
public void typeOperandsAndPerformSubOperation() {
performOperation(R.id.operation_sub_btn, "32.0", "16.0", "16.0");
}
@Test
public void typeOperandsAndPerformDivOperation() {
performOperation(R.id.operation_div_btn, "128.0", "16.0", "8.0");
}
@Test
public void divZeroForOperandTwoShowsError() {
final String expectedResult = getApplicationContext().getString(R.string.computationError);
performOperation(R.id.operation_div_btn, "128.0", "0.0", expectedResult);
}
@Test
public void typeOperandsAndPerformMulOperation() {
performOperation(R.id.operation_mul_btn, "16.0", "16.0", "256.0");
}
private void performOperation(int btnOperationResId, String operandOne,
String operandTwo, String expectedResult) {
// Type the two operands in the EditText fields
onView(withId(R.id.operand_one_edit_text)).perform(typeText(operandOne),
closeSoftKeyboard());
onView(withId(R.id.operand_two_edit_text)).perform(typeText(operandTwo),
closeSoftKeyboard());
// Click on a given operation button
onView(withId(btnOperationResId)).perform(click());
// Check the expected test is displayed in the Ui
onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult)));
}
}