Success is a concept, to be rich is an obligation, happiness is a kind of power.
What's in this lecture: test Cases
The test case is usually the program code with the desired running result, and the tester can determine whether the program is working correctly based on the final result of the operation.
I. benefits of test Cases
For example, you are maintaining a very large project, there are many functions, one day, according to the needs of one of the features you modify, a few days later, someone suddenly found that other features have problems, the final location is caused by the function you previously modified. Therefore, when the project is relatively large, it is generally necessary to write test cases. If we have written a test case for every feature of the project, every time we modify or add any functionality, we will run through all the test cases, and as long as any 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 a test project
Test engineering is usually not independent, but relies on an existing project, it is generally more common practice is to create a new tests folder under the existing project, the test project is stored here. :
The following is a Unit.java file for unit testing
public class Unit {public int add (int a,int b) {return a+b;} public int remove (int a,int b) {return a-A;}}
Here we create a test project for the demo project. Click File--new--other, then expand the Android directory, select Android test Project on the inside, then enter the name of the test project and select the path of the test project, usually we will select the path as the demo project under the Tests folder ,:
Continue to click Next and choose which project to create the test function for, here we select the Demo project above. The completion of the test project was created.
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.demo.test" android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8"/> <instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.demo "/> <application android:icon=" @drawable/ic_launcher " android:label=" @string /app_name "> <uses-library android:name=" Android.test.runner "/> </application></ Manifest>
where <instrumentation> and <uses-library> tags are automatically generated, indicating that this is a test project, in <instrumentation the package name of the test target is also specified by the Android:targetpackage property in the > tab.
2) for unit testing
setup () method is called before all test cases are executed, and there are some initialization operations here. teardown () method in All of the test cases are called after execution, and you can do some of the resource releases here.
So how do you write test cases? Just define a method that starts with test, and the testing framework will call this method automatically, and then in this method we can expect a run result in the form of an assert (assert) and 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.
The following is a Unittest.java file
public class UnitTest extends Androidtestcase {protected void SetUp () throws Exception {Super.setup ();} public void Testadd () {Unit unit=new unit (); int Sum=unit.add (2, 3); Assert.assertequals (5, sum);} protected void TearDown () throws Exception {Super.teardown ();}}
a Testadd () method is added here, in which the Assert class's Assertequals () method is invoked to assert that the current computed result is sum=5. You can now right-click the test project--run as--android JUnit test to run the test case, as a result:
If you change the code to assert.assertequals (4, sum); If there is an exception or error, the following conditions may occur:
pass by, learn to leave a message, the top of the chant ~~ take your time and enjoy it
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Writing test Cases