首页 > 解决方案 > 无法使用 Maven 运行测试来运行我的项目

问题描述

我正在尝试运行我的测试,但是我遇到了一个问题,即当我运行以下命令时我无法运行我的测试: mvn clean test

我的项目包含 3 个模块(见附图): 在此处输入图像描述

项目中的每个模块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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hackeruso</groupId>
  <artifactId>neo</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
      <module>automation-ui</module>
      <module>automation-api</module>
      <module>morpheus</module>
    </modules>

    <name>neo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <aspectj.version>1.8.10</aspectj.version>
      <testng.version>7.3.0</testng.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.qameta.allure</groupId>
      <artifactId>allure-testng</artifactId>
      <version>2.13.6</version>
    </dependency>
      <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>${testng.version}</version>
          <scope>test</scope>
      </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20</version>
          <configuration>
            <argLine>
              -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
            </argLine>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
          </dependencies>
        </plugin>

      </plugins>
    </pluginManagement>
  </build>
</project>

当我运行命令mvn test时,我收到以下消息:

No tests to run.

这张图片显示了我在哪里进行测试: 在此处输入图像描述

src/test/java/com/hackeruso/automation/ui/LoginTest

这是我的测试类的一个例子: --------------------EDIT-------------------- ----------------------

package com.hackeruso.automation.ui;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginTest extends BaseTest{

    @Test(dataProvider = "userDetailsProvider")
    public void loginTest(String username, String password){
        signIn(username, password);
    }

    @DataProvider(name = "userDetailsProvider")
    public Object[][] userDetailsProvider(){
        return new Object[][] {
                {"erezn@hackeruso.com", "*******"}
        };
    }

}

标签: javamaventestngpom.xml

解决方案


文档

例如,纯元数据(打包值为 pom)的项目仅将目标绑定到安装和部署阶段(有关某些打包类型的目标到构建阶段绑定的完整列表,请参阅生命周期参考)。

如您所见,只有安装和部署阶段(不是测试)对pom打包项目有效。

Java 代码不应该在那里,因为pom项目应该是纯粹的元数据。


推荐阅读