1, create an Android Project:junite project;
2. Create a Com.example.juint package in the Junite project
3. Create an Android test project: Testjunite (the project that made the test is junite)
4. Then add the Reference class library under the manifest manifest file <application> node of the Junite project:
<uses-library android:name= "Android.test.runner"/>
Add directives under the <manifest> node:
<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.example.junit"/>
5. Then junite a new package in the project: Com.example.junit.service, then create a class under the package: Calculate.java
class to add member functions
public class Calculate {/** * Add method * @param A * @param b * @return */public int Add (int a,int b) {return a+b;} /** * Subtraction Method * @param A * @param b * @return */public int sub (int a, int b) {return a-C;}}
6. Create a Test package: com.example.junit.test
Create a test class under the package: Testcalservice (inherited from: androidtestcase)
Then add the test code inside:
Public classTestcalservice extends Androidtestcase { Public voidTestadd () throws exception{//throw the program's accident to the test frameworkCalculate cal =NewCalculate (); inti = Cal.add (3,5); Assertequals (8, i); } /** * When the Testcalservice is created for the first time, do some initialization of the global variable operation*/@Overrideprotected voidsetUp () throws Exception {//TODO auto-generated Method StubSuper.setup (); } /** * When the test class is destroyed, do some wipe the butt of the Operation*/@Overrideprotected voidTearDown () throws Exception {//TODO auto-generated Method StubSuper.teardown (); }}
8. Then test in the outline view as shown in:
9, then can see the results of the test run, if shown as green, it means that the test results are correct, if the red is an error, the number of errors can be clicked on the error message to locate the wrong line number.
Android's lone formula: a powerful "test unit"