JUnit in Android

Source: Internet
Author: User

Android test framework is based on JUnit (www.junit.org), interested in can look at JUnit source code, I believe it will do a unit test in Android greatly helpful.

This article is just a primer on how JUnit was used for Android, and more of it was knocked out on the internet, and it was surprising that Samsung's official web site had introduced JUnit's articles,

Haha, so I took it over. I haven't done it before. Unit Test,junit is also recently contacted, record, learn while using it!!

English also OK can directly see the original (click me) it!!

The basic idea of JUnit:

A Create your own project (existing projects skip this step directly, starting with B)

B Create Android test project.

The steps, A, B are described below.

A Create your own project. Create a normal Android project where the Main.xml code

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "> <button android:id = "@+id/pounds" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layou T_alignparentright= "true" android:text= "Convert to Pounds"/> <edittext android:id= "@+id/inputvalue K "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:layout_toleftof=" @ Id/pounds "Android:hint=" No. of Kilos "/> <button android:id=" @+id/kilos "android:layout_width=" Wrap_content "Android:lay out_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_below= "@id/pounds" an Droid:text= "Convert to Kilos"/> <edittext android:id= "@+id/inputvaluep" android:layout_width= "fIll_parent "android:layout_height=" wrap_content "android:layout_below=" @id/pounds "Android:layout_tol eftof= "@id/kilos" android:hint= "No. of Pounds "/> <textview android:id=" @+id/resultview "android:layout_width=" Fill_parent "Android oid:layout_height= "Wrap_content" android:layout_below= "@id/kilos" android:text= "Result"/></relative Layout>

The related activity of the project, where sample code

Package Com.example.samplejunit;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.TextView;
public class Sample extends Activity implements Onclicklistener {button mckilos,mcpounds ; EditText Mkilos,mpounds; TextView mresult; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); mresult = (TextView) Findviewbyid (r.id.resultview); Mkilos = (EditText) Findviewbyid ( R.id.inputvaluek); mpounds = (EditText) Findviewbyid (R.ID.INPUTVALUEP); Mckilos = (Button) Findviewbyid (R.id.kilos); Mcpounds = (Button) Findviewbyid (r.id.pounds); Mckilos.setonclicklistener (this); Mcpounds.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.kilos:{double pounds = double.parsedouble ( Mpounds.gettext (). ToString ());d ouble kilos = pounds*0.4535;mresult.settext (new double (kilos). ToString ());} Break;case r.id.pounds:{double kilos = double.parsedouble (Mkilos.gettext (). toString ());d ouble Pounds = kilos*2.2046; Mresult.settext (new Double (pounds). ToString ());} Break;}}} 

When you run the sample



B Create Android test project.

A roughly simple introduction to creating Android Test project in Eclipse.

File→new→other→android Test project→next→new Android test Project, fill out Project Name and feel free to

→select Test Target, select the project to be tested, such as mine is Sample,→next all the way down → create success.


After that, a TestCase class Sampleunit is created, inherited from Activityinstrumentationtestcase2<sample>.


The next step is to rewrite the parent class's Method SetUp () TearDown (), and some other methods

SetUp () needs to initialize the environment before the use case runs . The method is always called first.

TearDown () for recycling resources and garbage cleanup

testviews (): samplejunit

Testkilos2pounds () test kilo turn pound correct

Testpounds2kilos () test pound turn kilo correct

SetUp () code (initialization variables and test environments)

Private Sample Msample;<span style= "White-space:pre" ></span>private TextView mtextview;<span style= " White-space:pre "></span>private button mckilo,mcpound;<span style=" White-space:pre "></span> Private EditText Mkilo,mpound;
<span style= "White-space:pre" ></span> @Overrideprotected void SetUp ()  throws Exception{super.setup ( ); msample = This.getactivity (); Mtextview = (TextView) Msample.findviewbyid (Com.example.samplejunit.r.id.resultview) ; Mckilo = (Button) Msample.findviewbyid (Com.example.samplejunit.r.id.kilos); mcpound = (Button) Msample.findviewbyid ( Com.example.samplejunit.r.id.pounds); Mkilo = (EditText) Msample.findviewbyid ( Com.example.samplejunit.r.id.inputvaluek); mpound = (EditText) Msample.findviewbyid ( COM.EXAMPLE.SAMPLEJUNIT.R.ID.INPUTVALUEP);}

TearDown () code

@Overrideprotected void TearDown () throws Exception{super.teardown ();}

TestView () code

@SmallTest public void Testviews () {Assertnotnull (getactivity ()); Assertnotnull (Mtextview); Assertnotnull (Mckilo); Assertnotnull (Mcpound); Assertnotnull (Mkilo); Assertnotnull (Mpound);}

About Smalltest,mediumtest,largetest's explanation

Small:this Test doesn ' t interact with anyfile system or network.

medium:accesses file systems on box Whichis running tests.

large:accesses external file Systems,networks, etc.


@SmallTestpublic void Testkilos2pounds () {mkilo.clearcomposingtext ();//touchutils--can Simulate Touch event Tapview--simulate touching the center of a viewtouchutils.tapview (this, Mkilo); SendKeys ("1");  Sending input to Mkilos as 1//click in the Buttontouchutils.clickview (this, mcpound); Simulate touching the center of view and releasedouble pounds;try {pounds = Double.parsedouble (Mtextview.gettext (). toSt Ring ());} catch (NumberFormatException e) {//Todo:handle exceptionpounds =-1;} Asserttrue ("1 kilo is 2.20462262 pounds", pounds > 2.2 && pounds < 2.3);}
@SmallTestpublic void Testpounds2kilos () {mpound.clearcomposingtext (); Touchutils.tapview (this, mpound); SendKeys ("1"); Touchutils.clickview (this, Mckilo);d ouble kilos;try {kilos = double.parsedouble (Mtextview.gettext (). toString ());} catch (NumberFormatException e) {//Todo:handle Exceptionkilos =-1;} Asserttrue ("1 pound is 0.45359 kilos", kilos > 0.4 && kilos < 0.5);}

The code is complete and it's time to run test project.

Sampleunit Right-click →run as→android Junit Test.



OK, JUnit's brief introduction to Android ends here, and the article on JUnit Android can click here.

If you need to see the source code, click here






JUnit in 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.