使用Maven对Android进行单元测试 [英] Unit testing Android with Maven

查看:74
本文介绍了使用Maven对Android进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对用Maven构建的android项目有疑问, 我们让它运行我们的活动测试,但是现在我们需要它来运行单元测试. 单元测试与活动测试位于同一项目中,如何在我们的pom.xml文件中进行设置?

I have a question about a android project that is build with Maven, We made it run our activity tests, but now we need it to run unit tests. The unit test is in the same project as the activity tests, how do i set it up in our pom.xml files?

这是父Pom.xml文件:

This is the Parent Pom.xml file:

<?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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>andersen.project</groupId>
  <artifactId>Hello-parent</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <name>Hello - Parent</name>

  <modules>
    <module>HelloWorld</module>
    <module>HelloWorldTest</module>
  </modules>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>2.2.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android-test</artifactId>
        <version>2.2.1</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
  <sourceDirectory>src</sourceDirectory>
  <testSourceDirectory>test</testSourceDirectory>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>com.jayway.maven.plugins.android.generation2</groupId>
          <artifactId>maven-android-plugin</artifactId>
          <version>2.8.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

这是应用程序pom.xml:

this is the application pom.xml:

<?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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>andersen.project</groupId>
    <artifactId>Hello-parent</artifactId>
    <version>1.0</version>
  </parent>

  <groupId>andersen.project</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0</version>
  <packaging>apk</packaging>
  <name>HelloWorld - Application</name>

  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>maven-android-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>4</platform>
          </sdk>
          <deleteConflictingFiles>true</deleteConflictingFiles>
          <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>

这是测试pom.xml文件

and this is the test pom.xml file

<?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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>andersen.project</groupId>
    <artifactId>Hello-parent</artifactId>
    <version>1.0</version>
  </parent>

  <groupId>andersen.project</groupId>
  <artifactId>helloworldtest</artifactId>
  <version>1.0</version>
  <packaging>apk</packaging>
  <name>HalloWorld - Integration tests</name>

  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android-test</artifactId>
    </dependency>
    <dependency>
      <groupId>andersen.project</groupId>
      <artifactId>helloworld</artifactId>
      <type>apk</type>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>andersen.project</groupId>
      <artifactId>helloworld</artifactId>
      <type>jar</type>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>maven-android-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>4</platform>
          </sdk>
          <deleteConflictingFiles>true</deleteConflictingFiles>
          <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>

我应该在父pom.xml中添加以下文本吗?:

Should i add the following text in the parent pom.xml?:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.1</version>
  <scope>test</scope>
</dependency>

并将其添加到其他poms吗?

And add it to other poms?

当我运行Maven时,输出为:

when i run maven the output is:

Junit.framework不存在

Junit.framework does not exist

推荐答案

添加 Surefire插件添加到测试pom.xml. Surefire运行单元测试.

Add the Surefire plugin to the test pom.xml. Surefire runs the unit tests.

这篇关于使用Maven对Android进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆