Android Junit test environment setup

one. Introduction to unit testing

In the process of Android development, it is often necessary to test business code. Anyone familiar with Java knows Junit. In Android, Google provides Junit, an automated framework optimized for Android based on Junit. Components can be obtained in Junit. , can simulate sending events and detect the correctness of program processing. To write test cases using junit in java, we need to inherit TestCase, and in android we need to inherit AndroidTestCase.

two. Environment setup

1. Business type

The add function is written in the business class, and Junit tests the correctness of the add function.

 

<span style="font-family:Times New Roman;">package com.mxy.service;

public class CalcService {

	public int add(int a,int b) {
		System.out.println("执行了加方法");
		return a + b;
	}
}</span>

2. Test class

The test class inherits the AndroidTestCase class and writes the testAdd function. Note that the function in the test class must start with test, the return value is void, and the type is public.

 

<span style="font-family:Times New Roman;">package com.mxy.test;

import com.mxy.service.CalcService;

import android.test.AndroidTestCase;

public class CalcServiceTest extends AndroidTestCase {
	
	public void testAdd() {
		CalcService cal = new CalcService();
		int result = cal.add(5,3);
		System.out.println(result);
		assertEquals(8, result);

	}

}</span>

3. Configure AndroidManifeset.xml

In the <application /> tag add:

 

<span style="font-family:Times New Roman;"><uses-library android:name="android.test.runner"/></span>

In the <mainfest /> tag add:

 

<span style="font-family:Times New Roman;"><instrumentation android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="com.uicc.cloudservice" android:label="Test for my app"/></span>

The package name of android:targetPackage must be consistent with the package of the manifest tag.

three. Problems and Solutions

1.  错误:Warning: No instrumentation runner found for the launch, using   android.test.InstrumentationTestRunner.  

Reason: The emulator cannot remember the configuration of Android manifest and needs to reset the running configuration at runtime.

The solution steps are as follows:  

(1). Right-click on the project or test class name and select properties  
(2). Select the name of the project to be run in the Run/Debug setting, click Edit on the right, enter the Edit launch configuration properties interface, in the Test tab Select android.test.InstrumentationTestRunner from the drop-down list behind the instrumentation runner.

(3). Just rerun the test unit. 

2. The package name of android:targetPackage must be consistent with the package of the manifest tag. Otherwise, a package not found error will occur.

Four. Reference URL

1.   http://blog.csdn.net/mengxiangyue/article/details/20568433  environment construction

2.  http://blog.csdn.net/you12345678901234567/article/details/7632807  error solution

 

Guess you like

Origin blog.csdn.net/li1500742101/article/details/45198617