JUnit basis for Android unit testing (1)

When creating a project with Android Studio, three folders main, test, and androidTest will be generated in the src folder at the same time.

write picture description here

test and androidTest are specifically for source-level white-box testing.
test: The folder is used to write unit tests that do not depend on the device environment, which can be run directly on the PC;
androidTest: The folder is used to write tests that need to be run on the device.

Classification

Functional testing: has nothing to do with UI, test IO operations, algorithms, processes, etc.
UI testing: test UI interaction logic, such as clicks, logins, etc.

Java unit testing framework: JUnit , Mockito , etc.;
Android: AndroidJUnitRunner , Espresso , etc.

This article mainly introduces the common annotations and assertions of JUnit and their basic use. Other testing frameworks will be introduced in subsequent blog posts.
JUnit

rely:

dependencies {
    testCompile 'junit:junit:4.12'
}

Common annotations and assertions:
write picture description here
write picture description here

Each test method in Junit3 must start with test, and annotations are added in Junit4, and there is no requirement for method names.
Example:
Create a calculation tool class

/**
 * Created by scy on 2018/4/26.
 */

public class Calculater {
    public int sum(int a, int b){
        return a+b;
    }

    public int substract(int a, int b){
        return a-b;
    }

    public int divide(int a, int b){
        return a/b;
    }

    public int multiply(int a, int b){
        return a*b;
    }
}

Then follow the steps below
write picture description here

write picture description here
Note: 1 Select the method to be tested 2 Click OK 3 Select the test class generation location

Automatically create test classes:
write picture description here

Pay attention to the import package import static org.junit.Assert.*;and then modify the code:


/**
 * Created by scy on 2018/4/26.
 */
public class CalculaterTest {

    private Calculater mCalculator;
    private int a = 1;
    private int b = 3;

    @Before//在测试开始之前回调的方法
    public void setUp() throws Exception {
        mCalculator = new Calculater();
    }


    @Test//我们需要测试的方法
    public void sum() throws Exception {
        int result = mCalculator.sum(a, b);
        // 第一个参数:"sum(a, b)" 打印的tag信息 (可省略)
        // 第二个参数: 3 期望得到的结果
        // 第三个参数  result:实际返回的结果
        // 第四个参数  0 误差范围(可省略)
        assertEquals("sum(a, b)",3,result,0);
    }

    @Test
    public void substract() throws Exception {
        int result = mCalculator.substract(a, b);
        assertEquals(-3,result,2);
    }

}

If the expected value and the actual value are within the error range, the test passes, otherwise the test fails!
Android Studio right-clicks the class -> run CalculaterTest, and all methods annotated with @Test in the use case will be executed. The above test results are as follows:
write picture description here
it shows that there is an error sum()method that expects to get 3 and actually returns 4, the error is 0, so it fails; while the second method substract()expects to get -3 and actually returns -2 but within the error range of 2, so the test passes.
If the code is changed sum() ->Assert.assertEquals(4,result);, the tests will all pass.
Here is a sample assertion from w3c:

/**
 * Created by scy on 2018/4/27.
 */
public class AssertTest {
    @Test
    public void testAssertions() {
        //test data
        String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = null;
        String str4 = "abc";
        String str5 = "abc";
        int val1 = 5;
        int val2 = 6;
        String[] expectedArray = {"one", "two", "three"};
        String[] resultArray = {"one", "two", "three"};

        //Check that two objects are equal
        assertEquals(str1, str2);

        //Check that a condition is true
        assertTrue(val1 < val2);

        //Check that a condition is false
        assertFalse(val1 > val2);

        //Check that an object isn't null
        assertNotNull(str1);

        //Check that an object is null
        assertNull(str3);

        //Check if two object references point to the same object
        assertSame(str4, str5);

        //Check if two object references not point to the same object
        assertNotSame(str1, str3);

        //Check whether two arrays are equal to each other.
        assertArrayEquals(expectedArray, resultArray);
    }
}

Reference
Testing Getting Started Guide
Official Testing Tutorial
Unit Testing in Android
w3c

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325492381&siteId=291194637