How to use JUnit to unit test eclipse in Android

Source: Internet
Author: User

How to use JUnit for unit testing on Android in our daily development AndroidApp, you need to constantly Test, so use JUnitTesting frameworks are especially important, and learning junit can speed up the development cycle of your application. There are two ways to build the JUnit test environment in Android. A. Create a new test class directly in the project that needs to be tested Integration steps:1. Add the following code to the Androidmanifest.xml file:
<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.junittest "android:label=" @string/app_name "></instrumentation>
<uses-library android:name= "Android.test.runner"/> The above code configuration is to add test instructions and introduce a test environment, the complete manifest file is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/ Res/android "package=" Com.example.junittest "android:versioncode=" 1 "android:versionname=" 1.0 ">< uses-sdkandroid:minsdkversion= "8" android:targetsdkversion= "/><instrumentation android:name=" Android.test.InstrumentationTestRunner "android:targetpackage=" Com.example.junittest "android:label=" @string/app _name "></instrumentation><applicationandroid:allowbackup=" true "android:icon=" @drawable/ic_launcher "Android:label=" @string/app_name "android:theme=" @style/apptheme "><uses-library android:name=" Android.test.runner "/><activityandroid:name=" com.example.junittest.MainActivity "android:label=" @string/ App_name "><intent-filter><action android:name=" Android.intent.action.MAIN "/><category android: Name= "Android.intent.category.LAUNCHER"/></intent-filter></activity></application></ Manifest>
2. Create a new test test class and inherit the Androidtestcase class, write a test method, and use assert assert within the test method to test the method to be tested. 3. Click on the outline view on the right, select the method to be tested and the >android JUnit test below to show how to use JUnit Unit Test 1 to create a simple test class first Calculator.java
Package Com.example.junittest;public class Calculator {public int add (int x,int y) {return x+y;} public int sub (int x,int y) {return x-y;} public int divide (int x,int y) {return x/y;} public int multiply (int x,int y) {return x*y;}}
2, create a test class, this class needs to inherit from the Androidtestcase sample code as follows:
Package Com.example.test;import Junit.framework.assert;import Com.example.junittest.calculator;import Android.test.androidtestcase;import Android.util.log;public class Calculatortester extends Androidtestcase {private Static final String TAG = CalculatorTester.class.getSimpleName ();p rivate Calculator calculator;/***  this method is Invoked before any of the test methods in the class.*  use it to set up the environment for the test (the test Fixtur E. You can use SetUp () to instantiate a new Intent with the action Action_main. You can then use this intent to start the Activity under test.*/@Overrideprotected void SetUp () throws Exception {LOG.E (TA G, "setUp"); calculator = new Calculator (); Super.setup ();} /*** Test Calculator's Add (int x, int y) method * Throws an exception to the test framework * @throws exception*/public void Testadd () throws exception{int result = C Alculator.add (3, 5); Assert.assertequals (8, result);} /*** test Calculator Divide (int x, int y) method * Throws an exception to the test framework * @throws exception*/public void Testdivide () throws ExCeption{int result = Calculator.divide (10, 0); Assert.assertequals (result);} /*** This method was invoked after all of the test methods in the class.* with it to does garbage collection and to reset the TES T fixture.*/@Overrideprotected void TearDown () throws Exception {log.e (TAG, "TearDown"); calculator = null; Super.teardown ();}}
A good habit is that each test method throws an exception: throws Exception, and asserts the result with an assert. 3. Run the test method through the outline view green bar indicates that the test passed, in the code we tested when 3+5 is equal to 8, so the result is definitely passed, if we change assertequals () 8 to 5, the following results will appear: The red bar indicates that the test failed, Click on the error message on the right to navigate to the line of code where the error occurred. Ii. Create a project dedicated to testingIt is recommended to create a specialized test project, as this can reduce the coupling of the code.  Integration steps: 1. New project, select new----> Other---->android Test Project 2. Select the project you want to test 3. And then the first way to build the test class is the same, and here it's easier to skip over. With this method, the relevant parameters have been automatically configured in the Androidmanifest.xml, which is more convenient without being configured.

How to use JUnit to unit test eclipse in Android

Related Article

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.