Android Unit Testing – Eclipse+JUnit

Hits: 0

Why write test code

Software engineering tells us that the earlier a problem with a program is detected, the lower the cost. Especially when writing large-scale programs, there are hundreds of variables, objects, and methods to effectively ensure the correctness of the program. General [single-step debugging] or logging can only ensure the correctness of some functions, but we At the same time, we should ensure that each function is completely correct, because if we use this function to expand the program in the future, but the implementation of this function itself has bugs, it is a very depressing thing.

Therefore, every time a function is written, all aspects of the function should be tested. Such tests are called [unit tests] . In the traditional programming method, unit testing is a very troublesome thing. You have to rewrite another program, call the method you need to test in this program, and carefully observe the running result to see if there is any error. Because of this trouble, programmers are not very enthusiastic about writing unit tests.

[JUnit] – If a project is very large and there are many things to be tested, if you don’t use Junit, then the workload is very large. One of the most basic functions of unit testing is to automate testing. Unit testing is to determine whether the result is correct by means of assertion, that is, use Assert.

How to write unit tests

Configure test permissions

First declare the permissions in the corresponding AndroidMainfest file

<!-- Apply for test permission--> 
    < instrumentation 
        android:name = "android.test.InstrumentationTestRunner" 
        android:targetPackage = "Project package name" />

        android:theme="@style/AppTheme" >

        <!-- Test package to be imported--> 
        < uses-library  android:name = "android.test.runner" />

            android:label="@string/app_name" >
                <action android: />

                <category android: />

Create test class

  1. New test class
  2. Configure test package name class name test scope
  3. Configure test functions

Write test cases

  1. write test function

public class test extends TestCase {
    int[] helper = { 7, 3, 2, 10, 5, 2, 6, 8 };
    int[] helperQuick = helper.clone();
    int[] correct = { 2, 2, 3, 5, 6, 7, 8, 10 };

    // Test if the merge sort function is correct 
    public  void  testMergeIntArray ()  {
        for (int i = 0; i < helper.length; i++) {
            assertEquals(helper[i], correct[i]);

    // Test if the quicksort median selection function is correct 
    public  void  test_quickSort ()  {
         int index = QuickSort.quickSort(helper, 0 , helper.length - 1 );
        assertEquals(index, 5);

    // Test if the quicksort function is correct 
    public  void  test_quick ()  {
        for (int i = 0; i < helperQuick.length; i++) {
            assertEquals(helperQuick[i], correct[i]);

  1. run the test

  2. View test results

more usage

  • @Ignore If there are some test functions that are temporarily unavailable but you don’t want to delete them, you can use this parameter to configure and ignore this test method when testing.
  • @Before The method that must be executed before the test, generally used to perform some initialization operations and some necessary assignment operations
  • @After finishing work performed after the test is completed
  • @Test(timeout = 1000) Set the test time (timeout=specified time) Specify a test time range. If the time is exceeded and the execution has not been completed, the execution fails
  • @Test(expected = ArithmeticException.class) Set the test exception. If you are sure that it will throw a division-by-zero exception in a test, but it does not throw in the actual operation, then you can also specify that there is a problem with this test

You may also like...

Leave a Reply

Your email address will not be published.