A brief summary of Android Test

Using Android studio can easily complete the code testing work, which is much more convenient than doing testing in Eclipse before. Here is a brief summary:
1. Create the test directory structure:
generally create the androidTest folder in the project root directory, and follow the src The package name structure creates a directory, and then you can create a test file, as shown in the figure:



You can also specify the test path in the module's build file:
        androidTest {
            java.srcDirs = ['androidTest/java']
            assets.srcDirs = ['androidTest/assets/']
        }


Second, create a test file: create a
new XXXTest.Java, which generally inherits InstrumentationTestCase.
Note that the name of the method inside, the test case must start with test, if you want to control the execution order, you need to add sequential numbers or letters after test, and it will be executed in the named order, such as: test1Query(), test2Insert ()etc;

public class UtilsTest extends InstrumentationTestCase {

    public void test1() throws Exception {
        assertTrue(Utils.isValidUrl("http://www.qq.com"));
        assertTrue(Utils.isValidUrl("www.qq.com/news?a=1"));
        assertFalse(Utils.isValidUrl("http://*.qq.com"));
        assertTrue(Utils.isValidUrl("http://www.qq.com/?a=1"));
        assertTrue(Utils.isValidUrl("www.qq.com:80"));
        assertTrue(Utils.isValidUrl("http://www.qq.com:80/news"));
        assertTrue(Utils.isValidUrl("www.qq.com/news"));
        assertTrue(Utils.isValidUrl("ftp://www.qq.com/news"));
        assertTrue(Utils.isValidUrl("ftp://www.qq/news")); // We can't judge that whether the domain suffix is valid.
    }

    public void test2() throws Exception {
        String[][] hosts =
                {{"baidu.com", "http://baidu.com/?v=1"},
                        {"qq.com", "https://qq.com/?v=1"},
                        {"qq.com", "ftp://qq.com/?v=1"},
                        {"qq.com:80", "https://qq.com:80/?v=1"}
                };

        for (int i = 0; i < hosts.length; i++) {
            assertEquals(hosts[i][0], Utils.extractHost(hosts[i][1]));
        }
    }

    public void test3() throws Exception {
        assertTrue(Utils.isValidRedirectUrl("http://www.qq.com"));
        assertTrue(Utils.isValidRedirectUrl("https://www.163.com"));
    }
}


3. Precautions:
If there are threads in the interface to be tested, it is generally displayed in the form of callback. At this time, pay attention to let the test thread wait, otherwise the main test thread will exit early, causing the test to fail, which can be controlled by the following methods:
    Object queryLock = new Object();

    private void waitProcessing(){
        try{
            synchronized (queryLock){
                queryLock.wait();
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
    }

    private void notifyProcessing() {
        synchronized (queryLock) {
            queryLock.notify();
        }
    }


4. Run: To
run the entire test case, you only need to right-click on the file -> run to
run a single case, click in the corresponding method, right-click -> run;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033379&siteId=291194637