我是靠谱客的博主 平淡犀牛,最近开发中收集的这篇文章主要介绍使用Maven构建Android白盒自动化测试项目,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用Maven构建Android白盒自动化测试项目

 

本文展示应用maven来构建一个简单的Android白盒测试的项目的过程。基本过程如下:

 

1.新建一个名为Demo的Android应用,需要注意的是代码文件位于Demosrcmainjava路径下。

2.针对Demo应用,新建一个Android测试项目DemoTest,测试项目的代码文件位于DemoTestsrcmainjava路径下。

3.将Demo和DemoTest两个项目放于同一个文件夹下,这里是都放在Parent这个文件夹下。

4.为Parent、Demo、DemoTest三个文件夹都分别添加一个pom.xml文件,在pom文件中指明三者之间的关系,即:Parent项目是主项目,DemoDemoTest是两个子项目,且DemoTest是一个用来测试Demo的项目。

 

详细过程如下:

首先准备好待测项目,待测项目实现一个简单的加法功能:


为了能够使用maven代码文件不是直接位于src目录下,而是位于src/main/java目录下。假如拿到的代码原本不是这样的结构,这里就需要做修改,比如写个脚本判断拷贝。

ParentDemosrcmainjavacomwinstonwolfedemoMainActivity.java 文件内容如下:

public class MainActivity extends Activity{
protected static final String LOG_TAG =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText value1 = (EditText) findViewById(R.id.value1);
final EditText value2 = (EditText) findViewById(R.id.value2);
final EditText result = (EditText) findViewById(R.id.result);
Button addButton = (Button) findViewById(R.id.addValues);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
try {
int val1 =Integer.parseInt(value1.getText().toString());
int val2 =Integer.parseInt(value2.getText().toString());
Integer answer = val1 +val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG,"Failed to add numbers", e);
}
}
});
Button multiplyButton = (Button) findViewById(R.id.multiplyValues);
multiplyButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
try {
int val1 =Integer.parseInt(value1.getText().toString());
int val2 =Integer.parseInt(value2.getText().toString());
Integer answer = val1 *val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG,"Failed to multiply numbers", e);
}
}
});
} 


 

准备好测试项目DemoTest,测试项目的代码文件内容如下:

ParentDemoTestsrcmainjavacomwinstonwolfedemotesttestCase1.java

 

import android.test.ActivityInstrumentationTestCase2;
import com.winstonwolfe.demo.MainActivity;
//被测试对象
import com.winstonwolfe.demo.R;
import android.widget.EditText;
public class testCase1 extendsActivityInstrumentationTestCase2<MainActivity> {
privateEditText result;
@SuppressWarnings("deprecation")
publictestCase1() {
super("com.winston.demo",MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
MainActivity mainActivity = getActivity();
result = (EditText) mainActivity.findViewById(R.id.result);
}
private static final String NUMBER_24 = "2 4 ENTER ";
private static final String NUMBER_74 = "7 4 ENTER ";
private static final String NUMBER_5_DOT_5 = "5 PERIOD 5 ENTER";
//PERIOD 小数点
private static final String NUMBER_NEG_22 = "MINUS 2 2 ENTER";
// -22
private static final String ADD_RESULT = "98";
private static final String ADD_DECIMAL_RESULT = "79.5";
private static final String ADD_NEGATIVE_RESULT = "52";
private static final String MULTIPLY_RESULT = "1776";
public void testAddValues() {
//两个整数相加
sendKeys(NUMBER_24);
sendKeys(NUMBER_74);
sendKeys("ENTER");
String mathResult = result.getText().toString();
assertTrue("Add result should be 98 " + ADD_RESULT + "but was "
+ mathResult,mathResult.equals(ADD_RESULT));
}
public void testMultiplyValues() { //两个整数相乘
sendKeys(NUMBER_24 + NUMBER_74 + " DPAD_RIGHT ENTER");
//D_PAD 上下左右导航键 KeyEvent.KEYCODE_DPAD_UP
到config.ini中启用虚拟机D_PAD
String mathResult = result.getText().toString();
assertTrue("Multiply result should be " + MULTIPLY_RESULT +" but was "
+ mathResult,mathResult.equals(MULTIPLY_RESULT));
}
}


 

Parent项目的pom文件内容如下:

<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Demo-Parent</name>
<modules>
<module>Demo</module>
<module>DemoTest</module>
</modules>
<properties>
<platform.version> 4.4.2
</platform.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<!-- Androlog is a logging and reporting library for Android -->
<dependency>
<groupId>de.akquinet.android.androlog</groupId>
<artifactId>androlog</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.1</version>
<configuration><sdk> <platform>19</platform> </sdk></configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>


 

Demo项目的pom文件内容如下:

<?xml version="1.0"encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>Demo</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.akquinet.android.androlog</groupId>
<artifactId>androlog</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src</nativeLibrariesDirectory>
<sdk>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


DemoTest项目的pom文件内容如下:

<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>DemoTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>DemoTest- Integration tests</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo</artifactId>
<type>apk</type>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.winstonwolfe.demo</groupId>
<artifactId>Demo</artifactId>
<type>jar</type>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


 

连接上手机,Cd到ParentDemoTest目录,执行测试命令:mvn install android:deployandroid:instrument

测试执行的结果如下:

 

此外,在 ParentDemoTesttargetsurefire-reports目录下也生成了xml格式的测试报告。

       

 

 

 

 

 

最后

以上就是平淡犀牛为你收集整理的使用Maven构建Android白盒自动化测试项目的全部内容,希望文章能够帮你解决使用Maven构建Android白盒自动化测试项目所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部