The first two articles in this series give a detailed introduction to menkey and JUnit. This article will begin to introduce the JUnit-based andriod testing framework, mainly from the network.
1. JUnit Overview
Android SDK 1.5 has included JUnit, but has never been familiar with it. I have learned cppunit and briefly analyzed its main framework in some open-source libraries using C ++, as follows:
In the course of learning the app apidemoes in Android SDK 1.6, I saw a test folder, which seems to have used JUnit. So I started to learn how to use JUnit in Android. The test code in the apidemoes \ test folder is relatively complicated. We will learn it step by step and gain a deep understanding. We will explain in detail how to read the test code later, JUnit will be analyzed in this article.
1.1 What is JUnit?
JUnit adopts the test-driven development method. That is to say, you should write the test code before development. It is mainly used to describe how the tested code is used and handle errors. Then you can write the code, and gradually test the code in the test code until it passes completely in the test code.
The four functions of JUnit are briefly described.
- Manage test cases. The modified code may affect the modified Code. Use JUnit to complete the modification. This is also the so-called testsuite in JUnit.
- Define the test code. This is also the so-called testcase in JUnit. Each testcase is defined according to the test requirements of the source code, and testcase is added to the corresponding testsuite for convenient management.
- Define the test environment. Before the testcase test, the "Environment" configuration will be called and used in the test. Of course, you can also directly define the test environment in the test case.
- Test results. For each normal or abnormal test, a clear definition is required for the running result and whether the result is as expected. JUnit provides powerful functions in this respect.
The above part is exactly the same as the process of debugging with ide. It only adds test case management, test result detection, and other functions to improve unit efficiency, this ensures the integrity of unit tests and clarifies the objectives of unit tests.
2. JUnit Example Analysis
In the previous preliminary analysis, we briefly understood the main features of JUnit:
Manage test cases;
Define the test code;
Define the test environment;
Test Results;
The following is a simple example based on the main functions:
Samplecalculator. Java
public class SampleCalculator{ public int add(int augend , int addend) {return augend + addend ;} public int subtration(int minuend , int subtrahend) { return minuend – subtrahend ;}}
Test Case (testcase ):
Import JUnit. framework. testcase; public class testsample extends testcase {private int A; private int B; private int R1, R2; void setup () /* Start the current test case-initialize the test environment */{A = 50; B = 20; R1 = 70; r2 = 30;} void teardown () /* Current Test Case test ends */{} public void testadd ()/* test the Add function of the samplecalculator class */{samplecalculator calculator = new samplecalculator (); int result = calculator. add (a, B); assertequals (R1, result);/* test result */} public void testsubtration () /* test the subtration function of the samplecalculator class */{samplecalculator calculator = new samplecalculator (); int result = calculator. subtration (a, B); assertequals (R2, result);/* test result */}}
In the above testsample test case, samplecalculator is fully tested and the test results are described as expected.
Testcase Management
import junit.framework.Test;import junit.framework.TestSuite;public class TestAll{public static Test suite(){TestSuite suite = new TestSuite(“TestSuite Test”);suite.addTestSuite(TestSample.class);return suite;}}
The above will add testsample to "testsuite test". In the future, if you select testsuite test during the selection of test cases, testsample will be added to the current test. If other functions are added to samplecalculator in the future, you only need to add the corresponding test code in testsample.
Finally, it should be noted that the management of testcase is completely interface-based, and JUnit will automatically generate the UI. Run the preceding test example. The JUnit interface is as follows:
You may need to download juint.
Packeage, finally to everyone: a bold attempt, you will find that programming can be so "beautiful ".
3. JUnit-based Android testing framework (JUnit. Framework Package)
Through the previous two sections, we have a preliminary understanding of JUnit. Next we will analyze JUnit data packets in depth. The entire JUnit data packet should be very powerful, but in general, not all projects need these data packets, but expand their own data packets based on some JUnit data packets, the android SDK is no exception. As for the complete JUnit package, we will not analyze it in detail here. Here we only parse the JUnit data packets contained in the android SDK and some data packets extended by the android SDK on JUnit, as shown below:
The most important of these packages are JUnit. Framework and Android. test. The former is the core package of JUnit, and the latter is andoid.
The SDK expands the packages based on JUnit. framework. We will focus on Parsing these two packages.
Parse the JUnit. framework package with the following structure:
Through this figure, you can see the main framework of JUnit clearly, and then go back to the example in the previous article to understand more about the previous example. A brief summary is as follows:
- Testsuit: testsuite is a set of test cases;
- Testcase: defines how to run multiple test cases;
- Testresult: Collects the results of a test case. The test results are divided into failures and errors. If the unanticipated assertions are failure, the error is like an unexpected problem caused by an arrayindexoutofboundsexception exception;
- Testfailure: Exceptions caught when the test fails;
- Assert: Set of assertion methods. Information is displayed when the assertion fails;
The relationship between testcase and testsuite is similar to the relationship between primitive objects and container objects. It is common in object-oriented languages C ++ and Java.
A simple example and a brief description of the process
Step 1: Implement testcase
// Inherit the parent class testcase; // define the variables used in the test; // initialize these variables in setup (); // clear these variables in teardown; public class mathtest extends testcase {protected double fvalue1; protected double fvalue2; protected void setup () {fvalue1 = 2.0; fvalue2 = 3.0 ;}// write the test unit code; public void testadd () {double result = fvalue1 + fvalue2; asserttrue (result = 5.0) ;}// run the test case. Here there are two methods to use: // static type: override runtes () and define test functions. The most commonly used anonymous class is Java, as shown below: testcase test = new mathtest ("add") {public void runtest () {testadd () ;}; test. run (); // dynamic type: Use reflection to implement runtest. It dynamically discovers and calls the method. In this case, the test method corresponding to the test case name is run, as follows: testcase = new mathtest ("testadd"); test. run (); // In contrast, the 2nd types are more object-oriented.
Step 2: Add testcase to testsuilt
Testsuite suite = new testsuite ();
Suite. addtest (New mathtest ("testadd "));
Because testsuite can automatically extract and run test units from testcase, you can also use the following method:
Testsuite suite = new testsuite (mathtest. Class );
A test case is completed. To learn more about JUnit. Framework, read it carefully in the android SDK.
After reading the code, let's take a closer look at the JUnit structure to see if it is clearer. Next we will go into depth to parse the Android. Test package.
4. JUnit-based Android testing framework (Android. Test package)
We have learned the JUnit. framework package. In this article, we will start to learn about the Android. Test package and learn about Android.
How the SDK extends the JUnit. Framework Package.
First, sort out the structure of the android. Test package, as shown in:
Previously, the relationship between testcase and testsuite was described, which is similar to the relationship between primitive objects and container objects. With this idea, we can look at the main line chart from the top down: first we can see the testcase class, then we can see instrumentationtestcase, androidtestcase, and finally we can see applicationtestcase, providertestcase2, servicetestcase, activitytestc. This is not android
The first three components in the system: Activity, provider, service, and broadcast. Let's take a look at the instructions in the android SDK:
These classes are not described here. They should be basically the same as what we usually use activity, service, and provider. The following example shows how to use these classes. We will use the previous j2se test example to implement it in Android:
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" ); }}
Compared with the JUnit example, we found that except the referenced package and parent class, the other parts are identical.
For specific code, see the next article.
After running the program on Android AVD, you can see the following results:
Summary
Here we briefly learned about the Android. Test package. In fact, this content is much more than this, including: 3 interfaces, 20 classes, and 2 errors, as shown below:
The android. Test package is only available for Android
Carefully read the SDK. By studying Android. Test, we can gain a deeper understanding of the components of the Android system: Activity, provider, and service.
References:
Android and JUnit