簡體   English   中英

想要使用 TestNg 包運行 maven 項目,但在執行項目時出錯

[英]Want to run a maven project with TestNg packages but getting error when executing the project

I have started to configure a maven project for selenium java and excluded the following junit dependency from pom.xml as I want to make it with TestNG. 所以包括 testng 依賴而不是它。

junit 依賴

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

pom.xml

<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>MobilePackage</groupId>
  <artifactId>MobileProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MobileProject</name>
  <url>http://maven.apache.org</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>
</properties>
    <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M4</version>        
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <dependencies>

  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-5</version>
  </dependency>


  </dependencies>
</project>

編譯項目(mvn已編譯)並且BUILD成功但當時發生問題,執行項目時($ mvn test)。終端顯示以下錯誤。

[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[3,23] package junit.framework does not exist
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[4,23] package junit.framework does not exist
.
.
.
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[11,13] cannot find symbol
[ERROR]   symbol: class TestCase
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[26,19] cannot find symbol
[ERROR]   symbol:   class Test
[ERROR]   location: class MobilePackage.AppTest

所以我發現問題存在於 AppTest.java 文件中。 那里仍然存在追隨者。

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

AppTest.java 代碼如下

package MobilePackage;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

如何擺脫這個問題。我是否需要為 TestNG 配置 apptest.java(我試過了,但是錯誤沒有消失)。我應該同時包括 testNG 和 Z587FEFE304B8E3505DE8295804Z3 嗎? 如果是這樣,那么它會在未來產生任何問題嗎? 請建議。

  1. 當您刪除 jUnit 依賴項時,相應的導入不起作用
  2. 創建 maven archtype 項目並刪除默認 App 文件夾結構
  3. 嘗試在 maven 確保觸發插件中添加此配置,以便 testng.xml 可以使用 mvn test 命令運行。

    org.apache.maven.plugins maven-surefire-plugin 3.0.0-M4 testng.xml

您不能只是將一個庫更改為另一個庫,並期望您的代碼仍然可以工作。 通過從 pom.xml 中刪除 JUnit ,您只需從項目中“關閉”這個庫。

但是,在您的 pom 中擁有一個 lib 而在您的代碼中沒有對該 lib 的任何引用是沒有意義的。 就像在您的情況下,您的代碼在很多地方都引用了 JUnit 庫的類。

您需要做的是仔細檢查您的代碼在哪里使用了 JUnit 實體,並在可能的情況下將它們更改為 TestNG 類似物。 如果不是,恐怕您將不得不擺脫該代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM