Using unit tests under Android

Source: Internet
Author: User

The Android.jar is self-contained in the JUnit unit test class, so as long as the configuration of the relevant unit test can be used in large project development when testing their corresponding modules very good.


Build an Android project that contains a class add that completes the addition operation and builds the appropriate junit to perform the relevant tests.

First, add the application inside the androidmanifest:

<uses-library android:name= "Android.test.runner"/>

A library used for well-known needs;

Next, add it outside the application:

<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.testjniandroid "android:label=" Tests for My App "/>

The targetpackage here needs to indicate the package name for the project.


In run as, you need to configure the running mode, select the Android JUnit Test, and then configure it accordingly.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/5B/wKiom1TQl8qDTih0AANU6JKKpMI767.jpg "title=" Configure run. png "alt=" wkiom1tql8qdtih0aanu6jkkpmi767.jpg "/>


Classes that need to be tested:

public class Add{int A,b;public Add (int a,int b) {this.a=a;this.b=b;} public int GetResult () {return a+b;}}

Write the class code for the test:

Import Junit.framework.testcase;public class Addtest extends TestCase {public void Testadd () {//fail ("not yet implemented "); Add x=new Add (1, 2); Assertequals (X.A, 1); Assertequals (x.b, 2);} public void Testgetresult () {//fail ("not yet implemented"); Add x=new Add (9,7); int Result=x.getresult (); SYSTEM.OUT.PRINTLN ("Result:" +result); Assertequals (result, 16);}}

The code is very simple, mainly to detect the results of the implementation is correct. Next is the run as Android JUnit Test to see the results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/5B/wKiom1TQmW6DOkw8AAEYolcquqk528.jpg "title=" Run the result. png "alt=" wkiom1tqmw6dokw8aaeyolcquqk528.jpg "/>


The above use of unit tests is very simple and is documented for later review. More detailed unit testing scenarios will be accumulated later.

This article from "thedevilking" blog, declined reprint!

Using unit tests under Android

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.