"Android advanced" junit Unit test environment construction and simple and practical

Source: Internet
Author: User

purpose of Unit testing

First, the functionality that JUnit unit testing is designed to do is to test that the written method is executed correctly, often for testing business methods.

Environment configuration for unit tests1. Under the application node of the androidmanifest manifest file, introduce the library used by the unit test
2. In the androidmanifest manifest file node parallel to the Application node, add the instrumentation node

The following is a complete configuration of the code
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"    package= "Com.example.junittest"    android:versioncode= "1"    android:versionname= "1.0" >    <uses-sdk        android:minsdkversion= "8"        android:targetsdkversion= "/>    <application        android:icon=" @drawable/ic_launcher        " Android:theme= "@style/apptheme" >        <!--Introducing the unit test's library--        <uses-library android:name= " Android.test.runner "/>    </application>    <!--configuration Package name--    <instrumentation        Android:name= "Android.test.InstrumentationTestRunner"        android:targetpackage= "Com.example.junittest" >    </instrumentation></manifest>

Description of the above code:the only place where you need to modify it is the following code
Android:targetpackage
The package name of the project to be tested is actually the package name of the current project.
definition of unit testsThe environment for unit tests is now complete, and here's how to test the methods in your code using unit tests
Let's assume that the Getnum method in the following code is tested
public class Demo {//To test the method, we set the return value to 10public int getnum () {return 10;}}

First create a class that inherits from Androidtestcase, and the code is as follows
Import android.test.androidtestcase;/** * Unit Test class *  * @author Zhaokaiqiang *  */public class Test extends Androidtes TCase {///Use this method to test the method that needs to be tested, be sure to throw Exception so that if an exception occurs, the JUnit test framework can react public void t () throws Exception {int i = new Demo (). GE Tnum ();//This is the use of assert assertion, in fact, we think the return result should be 9, but the return is actually 10, so this sentence will definitely throw exception assertequals (9, i);}}

The code comments above are clear and I won't explain them anymore.Unit Test RunThe code for unit tests is well defined, so how do we run it? We can run the unit test on our test machine by right-clicking on the method under the outline interface and selecting the following run as-->android Junit Test, because the menu after the order is not available .
after running, we can see the results of our operation in the JUnit interface .

at this point, we have completed a simple unit test.

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.