Android unit test JUnit

Source: Internet
Author: User

During project creation, unit tests are usually required to test whether the code you write runs normally. Some potential bugs can be killed at the birth stage of the program, in particular, for large-scale projects that cooperate with teams, unit tests and integration must be avoided at the end, it takes less than one hour a day to review your code using unit tests, which is more efficient than using a few days after a project cycle of one month or even longer to solve technical issues without technical details, this is also a good habit as a good programmer, but the key is to practice and develop good habits at ordinary times. We recommend that you read "45 excellent habits of efficient programmers".

The following example shows how to use unit test JUnit in Android. I will use two methods to provide examples for you. I hope this will help you in the development process:

1. directly create JUnit in the project.

For example, we have created a file with the following code, which contains some custom methods:

package com.example.junittestService;public class Service{    public void Str_Test(String str)    {        String temp = str.substring(2);    }        public int Simple_add(int a,int b)    {        return (a+b);    }}

 

2. To implement unit test JUnit, use code to check whether our code is pure. Below we write code to check the above methods.

In fact, the establishment of JUnit in the project is to create a class, and then write the corresponding method to test:

  

package com.example.junittestService;import junit.framework.Assert;import android.test.AndroidTestCase;public class TestService extends AndroidTestCase {    public void testStr_Test() throws Exception    {        Service test_service  = new Service();        test_service.Str_Test(null);    }        public void testSimple_Add() throws Exception    {        Service test_service  = new Service();        int result = test_service.Simple_add(1, 3);        Assert.assertEquals(4, result);    }}

 

Note that unit tests must inherit the androidtestcase parent class. In addition, when writing the unit test method, the method name is generally Test plus the method to be tested. We recommend that you add an exception to the androidtestcase file to handle the exception.

In the above Code, we instantiate the test_service object of a class to be tested, then call the method and record its return value, assert. assertequals (arg1, arg2), the first parameter is used to describe the expected value, that is, the value under normal circumstances, and the second parameter is the actual execution result, check whether the method is correct by comparison.

  

3. Add the dependency library of the unit test. Because we directly set up the unit test in the android project, add the following to androidmainfest. xml:


// Introduce components of the unit test dependency library into the project
<Uses-library Android: Name = "android. Test. Runner"/>
// Note that you need to add the correct position to the application instead of the activity.

 

<Instrumentation Android: Name = "android. Test. instrumentationtestrunner" Android: targetpackage = "com. example. junittest" Android: Label = "tests for junittest"/>
// Configure the Startup Device of the unit test framework
// This code is put out of the application. The targetpackage must be specified correctly, that is, the package to be tested. The label can be defined or not specified.

 

4. Run the unit test.

I believe that everyone is looking forward to using it to witness the effect after the unit test is completed. Then we run the unit test and run the android project differently.

First, open the eclipse outline window, which lists all our methods:

  

Right-click the method to be tested and choose run as-> Android JUnit test. You cannot choose the wrong method.

Then we can see the results in the JUnit window. Green indicates correct, and red indicates a problem.

Of course, the unit test written for the str_test method passes a null value and the running fails. The result in red indicates that there is a problem, the second simple_add method passes a 1 + 3 value, which is the same as the expected value 4. Green indicates that the operation is normal.

    

Unit testing is a mechanism for testing whether the method is correct by passing certain values to the method we write.

 

Another unit test method is to create an android test project independent of the android project. You must specify the project to be tested when creating the project, this method does not need to be added to the dependent library of the unit test. It is automatically configured for us. Since the time relationship is not described in detail, you can establish a test on your own if necessary.

Taking advantage of unit testing can greatly reduce our development tasks and minimize future bugs. I understand the above and hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.