Writing test cases for Android

Source: Internet
Author: User
Tags assert

Testing is a very important part of software engineering, and test cases can significantly improve the efficiency and accuracy of testing.  The app test case is an ordinary program code, usually with the desired results, and the tester can determine whether the program works correctly based on the final result of the operation. I believe most programmers do not like to write test cases, because this is a very tedious thing. Clearly run a program, observe the results of the operation can know the right and wrong, why also through the code to judge it?  Indeed, if it's just a trivial program, writing test cases is a bit of a superfluous, but when you're maintaining a very large project, you'll find it's very necessary to write test cases. For example, for example, you are actually maintaining a very large project with lots of numbers and countless functions. One day, your leader asks you to modify one of these features, which is not very difficult, and you quickly resolve it, and the test passes. But a few days later, suddenly someone found that other features have problems, the final location of the reason is that you have previously modified the function of the result! You can deaths it now. But do not think this is a fantasy, in large-scale projects, this situation is still very common.  Because many of the code in the project is common, you can modify a line of code to complete a function, and it is entirely possible that another feature will not work correctly. Therefore, when the project is relatively large, generally should write test cases. If we write a test case for every feature of the project, every time we modify or add any functionality, we run through all the test cases, and as long as any of the test cases do not pass, it means that the modified or new feature affects the existing functionality, so that you can find the problem early and avoid the occurrence of the accident. 1. Create test Project  Introducing so much, it's time to try it out, let's create a test project.  Before creating, you need to know that test engineering is usually not independent, but relies on an existing project, it is generally common practice to create a new tests folder under the existing project, the test project is stored here. Then let's create a test project for the project test that we just created. Tapping File→new→other in the eclipse's navigation bar opens a dialog to expand the Android directory, as shown in the Android Test project,1.   Clicking Next will pop up the dialog that created the Android test project, where we can enter the name of the test project and choose the path of the test project.  By convention, we select the path as the tests folder for the test project, as shown in 2.  Continuing to click Next, this will let us choose which project to create the test function, here of course choose Test, as shown in. Now click Finish to complete the creation of the test project. Observe the code for the Androidmanifest.xml file in the test project as follows:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.example.test.test" Android: Versioncode= "1" android:versionname= "1.0" ><USES-SDK android:minsdkversion= "8"/>< Instrumentationandroid:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "com.example.test"/ ><applicationandroid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" ><uses-library Android:name= "Android.test.runner"/></application></manifest>
The <instrumentation> and <uses-library> tags are automatically generated, indicating that this is a test project and also in the <instrumentation> tag via android: The Targetpackage property specifies the package name of the test target. 2, the unit test  To create a test project, let's take a unit test of the Broadcastbestpractice project.  Unit testing refers to the smallest functional module in the software test, if every unit in the software can pass the test, the robustness of the code is already very good. There is a Activitycollector class in the test project that is primarily used to manage all activities, so let's test the class.  First create a new Activitycollectortest class in the Testtest project and let it inherit from Androidtestcase, then rewrite the setup () and teardown () methods as shown below. public class Activitycollectortest extends Androidtestcase {@Override protected void setUp () throws Exception {super.  SetUp ();  } @Override protected void TearDown () throws Exception {Super.teardown (); }} where the Setup () method is called before all test cases are executed, there are some initialization operations here.  The TearDown () method is called after all test cases have been executed, and can be used to perform some resource release operations. So how do you write test cases? It is also very simple, just define a method that starts with test, and the testing framework will call this method automatically. Then we can expect a running result in the method with an assert (assert), and then compare it to the actual running result, so that a test case is completed.  The more extensive the test case coverage, the less likely the program will be to have bugs. For example, the Addactivity () method in Activitycollector is used to add activities to the collection, so we can write some test cases for this method, as shown in the following code:
public class Activitycollectortest extends Androidtestcase {@Overrideprotected void SetUp () throws Exception { Super.setup ();} public void Testaddactivity () {assertequals (0, ActivityCollector.activities.size ()); mainactivity main = new mainactivity (); Activitycollector.addactivity (main); Assertequals (1, ActivityCollector.activities.size ());} @Overrideprotected void TearDown () throws Exception {Super.teardown ();}}
As you can see, here we add a testaddactivity () method that invokes the Assertequals () method at the beginning of the method to assert that the current number of activities in Activitycollector is 0. Next, a loginactivity instance is created, and the Addactivity () method is called to add the activity to the Activitycollector, and then the Assertequals () method is invoked again to assert the  It is believed that the number of activities currently in Activitycollector is 1. Now you can right-click the test project →run as→android JUnit test to run the test case, and the result is that 51testingTestBird can help him, for example, we could quickly automate the app compatibility test on hundreds of phones. After the test can output a detailed test report, he through the log and can locate the problem. And our cloud phone can help him, there are more than 2000 real equipment, can be convenient for developers to remote real-computer debugging. With these tools in use, bugs can be repaired faster and time becomes more plentiful.

Writing test cases for 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.