Android's lone formula: a powerful "test unit"

Source: Internet
Author: User

1, create an Android Project:junite project;

2. Create a Com.example.juint package in the Junite project

3. Create an Android test project: Testjunite (the project that made the test is junite)

4. Then add the Reference class library under the manifest manifest file <application> node of the Junite project:

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

Add directives under the <manifest> node:

<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.example.junit"/>

5. Then junite a new package in the project: Com.example.junit.service, then create a class under the package: Calculate.java

class to add member functions

public class Calculate {/** * Add method * @param A * @param b * @return */public int Add (int a,int b) {return a+b;} /** * Subtraction Method * @param A * @param b * @return */public int sub (int a, int b) {return a-C;}}

  

6. Create a Test package: com.example.junit.test

Create a test class under the package: Testcalservice (inherited from: androidtestcase)

Then add the test code inside:

 Public classTestcalservice extends Androidtestcase { Public voidTestadd () throws exception{//throw the program's accident to the test frameworkCalculate cal =NewCalculate (); inti = Cal.add (3,5); Assertequals (8, i); }    /** * When the Testcalservice is created for the first time, do some initialization of the global variable operation*/@Overrideprotected voidsetUp () throws Exception {//TODO auto-generated Method StubSuper.setup (); }    /** * When the test class is destroyed, do some wipe the butt of the Operation*/@Overrideprotected voidTearDown () throws Exception {//TODO auto-generated Method StubSuper.teardown (); }}

8. Then test in the outline view as shown in:

9, then can see the results of the test run, if shown as green, it means that the test results are correct, if the red is an error, the number of errors can be clicked on the error message to locate the wrong line number.

Android's lone formula: a powerful "test unit"

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.