In this case, the use of unit testing mainly introduces two methods, 1, manually add configuration information, and then write the test class. 2. Create a test project from eclipse
1. Manually add the configuration information and then write the test class:
Classes to be tested:/src/com/example/unittest/unittestdemo1.java
Package com.example.unittest;
This class is the class to be tested
public class UnitTestDemo1 {
public void testing1 (String str) {
String TempStr = Str.trim ();
}
public int testing2 (int num1, int num2) {
return NUM1 + num2;
}
}
To configure in the manifest file Androidmanifest.xml:
The content marked with a red box is the configuration information that needs to be added, where the target property in <instrumentation> represents the package that contains the class to be tested, and the label property is arbitrary
After the configuration information is added, the test class is written, and the test class needs to inherit the Androidtestcase
Package com.example.unittest1;
Import Junit.framework.Assert;
Import Com.example.unittest.UnitTestDemo1;
Import Android.test.AndroidTestCase;
public class UnitTest extends androidtestcase{
public void TestTesting1 () throws exception{
UnitTestDemo1 UT1 = new UnitTestDemo1 ();
Ut1.testing1 ("Zhanghao");
}
public void TestTesting2 () throws exception{
UnitTestDemo1 UT1 = new UnitTestDemo1 ();
int tempresult = ut1.testing2 (1, 1);
Assert.assertequals (2, Tempresult);
}
}
Note that the methods in the test class are decorated with public void and cannot be changed.
After the test class is written, in the Outlook view of the class, select the appropriate method right-->run as->android Junit test to run
If the test passes, it is displayed in the JUnit view:
If the test fails, it is displayed as:
2. Create a test project from eclipse
Create an android Test Project in eclipse
After creating a new project, click Next, fill in the project name, then select an existing project and click Finish
This allows you to write test code in the newly created project to test the contents of the project under test. O (∩_∩) o~