Now integrated ADT Post Eclipse can create Android Test Project directly
:
Select the unit you want to test after naming, for example, I am ready to test SMS, I can choose
I built a new test project for Junitsms
I want to test the Testapp.java next simplest function in the SMS project
1 public class TestApp { 3 public int Add (int a, b) 4 { 5 int sum=a+B; 6 return sum; 7 8 }
Create a new test class apptest and inherit the Androidtestcase class, write a test method, and use assert assert within the test method to test the method to be tested.
Add the following code after the creation is successful
1 Packagecom.example.sms.test;2 3 ImportJunit.framework.Assert;4 ImportCmo.example.test.testapp;5 Importandroid.test.AndroidTestCase;6 ImportAndroid.util.Log;7 8 Public classApptestextendsAndroidtestcase {9 Private Static FinalString tag= "Apptest";Ten One Public voidAppshow ()throwsthrowable{ A -TestApp tp=NewTestApp (); - intRslt=tp.add (3, 7); theAssert.assertequals (10, rslt); -LOG.I (Tag, "Appshow"); - } - + -}
To join log is to learn to view and output the log information, and then select the method in the right outline area run as-->android Junit Test
If the test is normal,
But if you change the Apptest.java to
Assert.assertequals (one, rslt);
Re-test run the following results
Android Unit Test junit Configuration and usage