Модульное тестирование Android с Maven - PullRequest
8 голосов
/ 08 марта 2011

У меня есть вопрос по поводу проекта Android, который построен с Maven, Мы заставили его запускать наши тесты активности, но теперь нам нужно запустить модульные тесты. Модульный тест находится в том же проекте, что и тесты активности, как мне его настроить в наших файлах pom.xml?

Это файл Parent.om.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>
  <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:

<?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

<?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?:

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

И добавить его в другие помпы?

когда я запускаю maven, вывод:

Junit.framework не существует

Ответы [ 2 ]

7 голосов
/ 02 марта 2013

Я столкнулся с той же проблемой. Это было вызвано неправильной конфигурацией в родительском пом. Вы должны удалить тег области действия в разделе dependencyManagement.

Как это:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android-test</artifactId>
      <version>2.2.1</version>
    </dependency>
  </dependencies>
</dependencyManagement>
0 голосов
/ 08 марта 2011

Добавьте плагин Surefire в тест pom.xml.Surefire запускает юнит-тесты.

...