gpt4 book ai didi

android - 使用 Maven 对 Android 进行单元测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:51:31 24 4
gpt4 key购买 nike

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

这是父 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>
<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>

并将其添加到其他 poms 中?

当我运行 maven 时,输出是:

Junit.framework 不存在

最佳答案

我遇到了同样的问题。这是由父 pom.xml 中的错误配置引起的。您必须删除 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>

关于android - 使用 Maven 对 Android 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5231686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com