Android????????JUnit???е??????
???????????? ???????[ 2014/5/19 11:29:43 ] ????????Android JUnit ???????
????2.??????????????????AndroidTestCase????д?????????????????????????assert?????????????????
????3.??????????????????????????????????run as --->Android JUnit test
???????????????????????????????????JUnit???????
????1??????????????????Calculator.java
package com.example.junittest;
public class Calculator {
public int add(int x??int y){
return x+y;
}
public int sub(int x??int y){
return x-y;
}
public int divide(int x??int y){
return x/y;
}
public int multiply(int x??int y){
return x*y;
}
}
|
????2???????????????????????????AndroidTestCase
??????????????£?
package com.example.test;
import junit.framework.Assert;
import com.example.junittest.Calculator;
import android.test.AndroidTestCase;
import android.util.Log;
public class CalculatorTester extends AndroidTestCase {
private static final String TAG = CalculatorTester.class.getSimpleName();
private Calculator calculator;
/**
* This method is invoked before any of the test methods in the class.
* Use it to set up the environment for the test (the test fixture. You can use setUp() to instantiate a new Intent with the action ACTION_MAIN. You can then use this intent to start the Activity under test.
*/
@Override
protected void setUp() throws Exception {
Log.e(TAG?? "setUp");
calculator = new Calculator();
super.setUp();
}
/**
* ????Calculator??add(int x?? int y)????
* ?????????????
* @throws Exception
*/
public void testAdd() throws Exception{
int result = calculator.add(3?? 5);
Assert.assertEquals(8?? result);
}
/**
* ????Calculator??divide(int x?? int y)????
* ?????????????
* @throws Exception
*/
public void testDivide() throws Exception{
int result = calculator.divide(10?? 0);
Assert.assertEquals(10?? result);
}
/**
* This method is invoked after all the test methods in the class.
* Use it to do garbage collection and to reset the test fixture.
*/
@Override
protected void tearDown() throws Exception {
Log.e(TAG?? "tearDown");
calculator = null;
super.tearDown();
}
}
|
????????????????????????????????:throws Exception????????Assert???????ж????
????3??????????????в??????
????????????????????????????????????3+5??????8???????????????????????????assertEquals()?е?8???5????????????????:
???????????????????????????????????????λ???????????С?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11