Android Learning 6-use of unit tests

Source: Internet
Author: User

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~

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.