Android Unit Test junit Configuration and usage

Source: Internet
Author: User

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

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.