"Android advanced" junit Unit test environment Construction and simple and useful

Source: Internet
Author: User

The purpose of unit test

First of all. The function that JUnit unit tests to realize is to test whether the method that writes well is able to run correctly, generally is used to test the business method more.

Environment configuration of unit test1. Under the application node of the androidmanifest manifest file. Introduction of the library used in unit testing
2. The androidmanifest manifest file is in a node parallel to the application node. Join 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 Unit test--        <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:only the following code is needed to change the location
Android:targetpackage
The package name of the project to test, in fact the package name of the current project can be
definition of Unit testThis is the end of the unit test environment. Here's how to test the method in the code using the Unit test
If we test the Getnum method in the following code
public class Demo {//To test the method, we set the return value to 10public int getnum () {return 10;}}

First create a class. Inherit from Androidtestcase, code such as the following
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 assume an exception. The JUnit test framework talent reacts public void t () throws Exception {int i = new Demo (). Getnum ();//This is the use of assert assertions, 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 above code gaze is very clear. I won't explain it anymore.

Unit Test Execution the code for the unit test is well defined. So how do we do it? We right-click on the outline interface. Selecting the following run as-->android Junit test will enable the unit to be tested on our test machine, since the menu after the point out cannot be
Once executed, we will be able to see the results of our execution in the JUnit interface.

At this point, we are finished with a simple unit test.

"Android advanced" junit Unit test environment Construction and simple and useful

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.