Android unit test using AndroidTestCase

Source: Internet
Author: User

This article describes how to use intrumentation for android unit testing. In fact, there is also a way to use AndroidTestCase for unit testing. intrumentationTestCase and AndroidTestCase are both Junit. framwork. the subclass of TestCase.
 

To use AndroidTestCase, follow these steps:
1. add your own test case code so that they can inherit from AndroidTestCase.

2. define your own testSuite class to manage test cases.

3. define your own testRunner for testing.

Next, let's take a look at the android classes and interfaces involved in this method.
AndroidTestCase
Android test cases classes needs to be derived from this class, instead of from junit. framework. testCase. the main difference between the two is that Android test cases provides a method getContext () to obtain the current context variable, which is very important in android testing, because many android APIs require context.

Main AndroidTestCase members:

SetUp () // Sets up the fixture, for example, open a network connection.

TearDown () // Tears down the fixture, for example, close a network connection.

TestAndroidTestCaseSetupProperly ()

TestSuite (in package junit. package)
A TestSuite is a collection of test cases. Use testsuite to better manage test Cases

 

Main members of TestSuite:

Void addTest (Test test) // Adds a test to the suite.

Void addTestSuite (Class testClass) // Adds the tests from the given class to the suite

The following is an example of adding test case to test suite:

TestSuite suite = new TestSuite (); suite. addTest (new MathTest ("testAdd"); // Adds a test to the suite. suite. addTest (new MathTest ("testDivideByZero"); or you can use addTestSuite () to add: suite. addTestSuite (MathTest. class); TestListener (in package junit. framework)
 

This is an interface used to listen to the test process

There are four Public Methods:

Abstract void addError (Test test, Throwable t)

An error occurred.

Abstract void addFailure (Test test, AssertionFailedError t)

A failure occurred.

Abstract void endTest (Test test)

A test ended.

Abstract void startTest (Test test)

A test started.

AndroidTestRunner
Inherited from class junit. runner. baseTestRunner, but it does not provide the ui, or even a console-based UI, so if you want to view the test results well, you need to handle the callback function from test runner by yourself. I will give an example to illustrate it later.

AndroidTestRunner:

SetTest ();

RunTest ()

AddTestListener ()

SetContext ()

To use AndroidTestRunner, you must add the permission in manifest. xml:
<Uses-library android: name = "android. test. runner"/>

Finally, we will use an example to demonstrate:

1. Write a test case:

MathTest. java


Package com. ut. prac;
 
Import android. test. AndroidTestCase;
Import android. util. Log;

Public class MathTest extends AndroidTestCase {
Protected int i1;
Protected int i2;
Static final String LOG_TAG = "MathTest ";

Public void setUp (){
I1 = 2;
I2 = 3;
}
Public void testAdd (){
Log. d (LOG_TAG, "testAdd ");
AssertTrue (LOG_TAG + "1", (i1 + i2) = 5 ));
}

Public void testAndroidTestCaseSetupProperly (){
Super. testAndroidTestCaseSetupProperly ();
Log. d (LOG_TAG, "testAndroidTestCaseSetupProperly ");
}
}
 


2. Define a test suite class.

ExampleSuite. java

 

 

 


Package com. ut. prac;

Import junit. framework. TestSuite;

Public class ExampleSuite extends TestSuite {
Public ExampleSuite (){
AddTestSuite (MathTest. class );
}
}

 

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.