[转]Robotium 数据驱动测试框架
本文转自:http://www.cnblogs.com/morebetter/archive/2011/05/26/2058255.html
refer to http://controlingquality.blogspot.com/2011/02/design-data-driven-framework-around.html
被测试代码是简易计算器,代码: /Files/morebetter/android code/AndroidCalculator.rar
1. 数据驱动测试架构
-
测试数据源:TestData.csv
First Value | Second Value |
10 | 1.5 |
20 | 3 |
-
第一个输入框从First Value中读数据
-
第二个输入框从Second Value中读数据
-
点击Multiply
-
比较测试结果和期望结果是否一致,将结果写到文件里
2. 创建数据源文件
格式如上图
3. 把数据源文件上传到Emulator上
-
在被测试代码中创建res/raw/files文件夹。这样files文件夹就能被上传到Emulator上了
-
用Eclipse—Run As—Android Application 运行被测试代码
-
在Eclipse上加载DDMS,点击File Exploer,浏览Emulator-5554的所有文件
- 打开/data/data/com.calculator/files, 点击右侧上传到device的按钮,将csv文件上传到emulator上
4. 编辑测试case, 代码为:/Files/morebetter/android code/AndroidCalculatorTestApk.rar
5. 运行测试case
6. 将测试结果写到文件里,该文件存放在/data/data/com.calculator/files 下面
7. 将测试结果导入到本地电脑中
就像作者说的,这个只是个简单的框架,大家可以根据自己的不同需求进行修改。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.jayway.android.robotium.solo.Solo;
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
@SuppressWarnings("unchecked")
public class TestCalculatorApk extends ActivityInstrumentationTestCase2
{
private static final String TARGET_PACKAGE_ID="com.calculator";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.calculator.Main";
private static Class<?> launcherActivityClass;
Workbook wb;
WritableWorkbook copy ;
WritableSheet sheet ;
Boolean FuntionResult = false;
Cell rowData[] = null;
private String[] FirstValue;
private String[] SecondValue;
private String[] ExpectedResult;
int rowCount = '0';
WorkbookSettings ws = null;
Workbook workbook = null;
Sheet s = null;
String[] TestResults;
static{
try
{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class not found" +e);
}
}
public TestCalculatorApk() throws ClassNotFoundException
{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception
{
solo = new Solo(getInstrumentation(),getActivity());
}
public void CanOpenSettings()
{
solo.pressMenuItem(0);
}
public void testDisplayBlackBox() {
getTestDataFile();
TestResults = new String[rowCount];
for (int i = 1; i < rowCount; i ++ )
{
//Enter value in first editfield
solo.enterText(0, FirstValue[i].toString());
//Enter value in second editfield
solo.enterText(1, SecondValue[i].toString());
//Click on Multiply button
solo.clickOnButton("Multiply");
//Verify that resultant of first editfield and second editfield
float First = Float.parseFloat(FirstValue[i].toString());
float Second = Float.parseFloat(SecondValue[i].toString());
float expectedResult = First*Second;
//float expectedResult = Float.parseFloat(ExpectedResult[i].toString());
float actualResult = Float.parseFloat(solo.getText(9).getText().toString());
System.out.println("Value in Resultant Field "+solo.getText(9).getText().toString());
System.out.println("Expected Value "+expectedResult);
assertNotNull(solo.getText(9));
assertEquals("Values are not same", expectedResult, actualResult);
TestResults[i] = "Pass";
solo.clearEditText(0);
solo.clearEditText(1);
}
writeResults(TestResults);
}
public void readTestData(InputStream fileInputStream) throws Exception {
ws = null;
workbook = null;
s = null;
int columnCount = '0';
int totalSheet = 0;
String firstSheet = "TestCases";
String secondSheet = "TestScenarios";
try {
ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
workbook = Workbook.getWorkbook(fileInputStream, ws);
totalSheet = workbook.getNumberOfSheets();
if(totalSheet > 0) {
if (!workbook.getSheet(0).getName().equals(firstSheet))
{
System.out.println ("contents are not fine");
}
if (!workbook.getSheet(1).getName().equals(secondSheet))
{
System.out.println ("contents are not fine");
}
}
else
System.out.println ("There is not any sheet available.");
s = workbook.getSheet(1);
rowCount = s.getRows();
FirstValue = new String[rowCount]; //First Value array size
SecondValue = new String[rowCount];//Second Value array size
ExpectedResult = new String[rowCount];
columnCount = s.getColumns();
rowData = s.getRow(0);
if (rowData[0].getContents().length() != 0)
{
for (int i =1; i < rowCount; i ++)
{
rowData = s.getRow(i);
if (rowData[5].getContents().equals("Yes"))
{
System.out.println("Executed: " + rowData[1].getContents());
FirstValue[i] = rowData[2].getContents().toString();
SecondValue[i] = rowData[3].getContents().toString();
ExpectedResult[i] = rowData[4].getContents().toString();
}
else
{
System.out.println("We will skip "+rowData[1].getContents());
}
}
System.out.println("Success");
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
public void writeResults(String[] results)
{
FileOutputStream outputStream = null;
try{
outputStream = getActivity().openFileOutput("TestResultData.txt", Context.MODE_WORLD_READABLE);
for (int i=0; i <results.length; i ++)
{
outputStream.write(results[1].toString().getBytes());
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();}
}
}
}
public void getTestDataFile()
{
FileInputStream fs = null;
try {
fs = getActivity().openFileInput("TestData.csv");
readTestData(fs);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void tearDown() throws Exception
{
try
{
solo.finalize();
}
catch (Throwable e)
{
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
posted on 2011-08-06 11:28 freeliver54 阅读(2166) 评论(0) 编辑 收藏 举报