How to perform android unit testing

Source: Internet
Author: User

How to perform android unit testing

 
 

  1. Add the following to menifest. xml:

    <Application>Add:

    <Uses-library Android: Name = "android. Test. Runner"/>

    <Application>Join outside:

    <Uses-Permission Android: Name = "android. Permission. run_instrumentation"/>

    <Instrumentation Android: Name = "android. Test. instrumentationtestrunner" Android: targetpackage = "name. feisky. Android. test"

    Android: Label = "test for my app"/>

  2. Write unit test code: it must inherit from androidtestcase class

    Package name. feisky. Android. test;

     
     

    Import Android. Test. androidtestcase;

    Import JUnit. Framework. Assert;

     
     

    Public class mytest extends androidtestcase {

    Private Static final string tag = "mytest ";

     
     

    Public void testsave () throws throwable

    {

    Int I = 4 + 8;

    Assert. assertequals (5, I );

    }

     
     

    Public void testsomethingelse () throws throwable {

    Assert. asserttrue (1 + 1 = 12 );

    }

     
     

    }

  3. Run the test

    IntellijMedium:


     
     


    In eclipse: Right-click Run as Android JUnit Test


    Command line tool:

    ADB shell am instrument-W name. feisky. Android. Test/Android. Test. instrumentationtestrunner

     
     

You can also create a test project for testing.

 
 

  1. New>Project>Android>Android test project.


  1. Add test case class

    Add a new class and set the base class to Android. Test. activityinstrumentationtestcase2

  2. Add Constructor

    Add setup () method, which initializes variables and test environments before all tests.


    @ Override

    Protected
    Void setup ()
    Throws
    Exception
    {

    Super. Setup ();

    Mactivity =
    This. getactivity ();

    Mview =
    (Textview) mactivity. findviewbyid (COM. example. helloandroid. R. Id. textview );

    Resourcestring = mactivity. getstring (COM. example. helloandroid. R. String. Hello );

    }

  3. Add the testpreconditions () method, check the initialization environment, and only execute once

    Public
    Void testpreconditions ()
    {

    Assertnotnull (mview );

    }

  4. Add Unit Test


    Public
    Void testtext ()
    {

    Assertequals (resourcestring, (string) mview. gettext ());

    }

  5. Test Run as...> Android JUnit Test
Related Article

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.