[android] the JUnit test framework configuration under Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>Our business code is typically placed under a new package, and this business class cannot be accessed by right-clicking the run as <span style="font-family: Calibri;">Java application</span><span style="font-family: 宋体;">, because</span> <span style="font-family: Calibri;">android</span> <span style="font-family: 宋体;">projects can only be run on the phone</span> <span style="font-family: Calibri;">Dalvak</span> <span style="font-family: 宋体;">inside the virtual machine</span></p></p><p><p></p></p><p><p>Create a new package, write test class, test class need to inherit <span style="font-family: Calibri;">androidtestcase</span> <span style="font-family: 宋体;">class, write test method, need</span> <span style="font-family: Calibri;">throws exception</span> <span style="font-family: 宋体;">throw exception to test framework, test method inside General </span> <span style="font-family: Calibri;">New to</span> <span style="font-family: 宋体;">test the class, invoke its method, and then assert the result,</span><span style="font-family: Calibri;">assertequals (</span> <span style="font-family: 宋体;">estimate</span> <span style="font-family: Calibri;">,</span> <span style="font-family: 宋体;">actual result</span> <span style="font-family: Calibri;">) </span></p></p><p><p></p></p><p><p>In the <span style="font-family: Calibri;">outline</span> <span style="font-family: 宋体;">window (</span><span style="font-family: Calibri;">window=>show view=>outline</span><span style="font-family: 宋体;">), Select the method right-click</span> <span style="font-family: Calibri;">Run as Android JUnit Test</span></p></p><p><p>This will cause an error</p></p><p><p>[2016-02-27 21:29:54- <span style="font-family: 宋体;">Unit Test</span> <span style="font-family: Calibri;">]</span> <span style="font-family: 宋体;">Unit Test</span> <span style="font-family: Calibri;">does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.te St.runner</span> in its androidmanifest.xml</p></p><p><p>Need to configure the <span style="font-family: Calibri;">instrumentation</span> <span style="font-family: 宋体;">instruction set</span> in the manifest file</p></p><p><p><!-- <span style="font-family: 宋体;">Instruction set under the</span> <span style="font-family: ‘Courier New‘;">manifest</span> <span style="font-family: 宋体;">node </span> <span style="font-family: ‘Courier New‘;"></span> --</p></p><p><p><!-- <span style="font-family: 宋体;">Test case, name is fixed, target package name </span> <span style="font-family: ‘Courier New‘;"></span> --</p></p><p><p><instrumentation</p></p><p><p>Android:name= "android.test.InstrumentationTestRunner"</p></p><p><p>Android:targetpackage= "com.tsh.junit" ></p></p><p><p></instrumentation></p></p><p><p><!-- <span style="font-family: 宋体;">Libraries used in the</span> <span style="font-family: ‘Courier New‘;">application</span> <span style="font-family: 宋体;">node </span> <span style="font-family: ‘Courier New‘;"></span> --</p></p><p><p><uses-library android:name= "android.test.runner"/></p></p><p><p>The Xml <span style="font-family: 宋体;">writes the comment </span> <span style="font-family: Calibri;">ctrl+shift+/</span><span style="font-family: 宋体;">,</span></p></p><p><p></p></p><p><p>Junit <span style="font-family: 宋体;">Prints a green bar, stating that there is no error, if there is error printing red bar, error chasing inside there are error messages, such as assertion errors, etc., if the information in the manifest file cannot be remembered, then please do so,</span><span style="font-family: Calibri;">new + Project Android Android Test project = + Select Test Target</span> <span style="font-family: 宋体;">after completion there will be a project with the manifest file</span></p></p><p><p></p></p><p><p><span style="font-family: 宋体;">Java code:</span></p></p><p><p></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.tsh.junit.test;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">com.tsh.junit.service.CalcService;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.test.AndroidTestCase;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Calservicetest<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Androidtestcase {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Testadd ()<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">Exception{calcservice Service</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Calcservice (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Res=service.add (3, 5<span style="color: #000000;"><span style="color: #000000;">); Assertequals (</span></span>8<span style="color: #000000;"><span style="color: #000000;">, res); }}</span></span></pre><p><p></p></p><p><p>Manifest file:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Manifest</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;"></span> package</span><span style="color: #0000ff;"><span style="color: #0000ff;">= "com.tsh.junit"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:versioncode</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "1"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:versionname</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "1.0"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">instruction set under the manifest node</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">test case, name is fixed, target package name</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Instrumentation</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.test.InstrumentationTestRunner"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:targetpackage</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "com.tsh.junit"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Instrumentation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">USES-SDK</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:minsdkversion</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "8"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:targetsdkversion</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "all"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Application</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:allowbackup</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "true"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:icon</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@drawable/ic_launcher"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:label</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/app_name"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:theme</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@style/apptheme"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">Library of functions used, under the application node</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">uses-library</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.test.runner"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Activity</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Mainactivity "</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:label</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/app_name"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Intent-filter</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Action</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.intent.action.MAIN"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">category</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.intent.category.LAUNCHER"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Intent-filter</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Activity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Application</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Manifest</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p></p></p><p><p>[android] the JUnit test framework configuration under Android</p></p></span>

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.