簡體   English   中英

junit:未找到測試

[英]junit: no tests found

我繼承了一個 Java 項目,並且是 Java 開發的新手。 我覺得讓我適應代碼的一個好方法是圍繞它編寫一些測試。 我正在使用 IntelliJ 編寫代碼。

我現有的項目有這樣的文件夾結構:

/myProject
  /src
    /main
      /java
        /com.lexcorp
          /core
            /email
              /providers
                emailProvider.java

我創建了一個新項目,該項目將為此項目進行測試。 我希望這個項目同時進行單元測試和集成測試。 目前,我的新項目的結構如下:

/myProjectTests
  /src
    /main
      /java
        /com.lexcorp.core.email.providers
          emailProviderTest.java

emailProviderTest.java 文件如下所示:

package com.lexcorp.core.email.providers;

import junit.framework.TestCase;
import org.junit.Test;

public class EmailProviderTest extends TestCase {

    private final String username = "[testAccount]";

    private final String password = "[testPassword]";

    @Test
    public void thisAlwaysPasses() {
        assertTrue(true);
    }
}

該項目具有具有以下屬性的運行/調試配置:

  • 測試種類:全包
  • 搜索測試:在整個項目中

當我運行此配置時,我收到一條錯誤消息:

junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我不明白為什么我會收到一個錯誤,歸結為:“未找到測試”。 雖然我的項目結構不同,但操作系統上的文件夾結構匹配(這是另一件讓我感到困惑的事情)。 為什么我會收到此錯誤以及如何修復它?

我也收到這個錯誤:

junit.framework.AssertionFailedError:在...中找不到測試

原因是我忘記指定

defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

同步項目后,它找到了測試。 所以也許它可以幫助別人。

擴展junit.framework.TestCase是舊的 JUnit 3 實現測試用例的方法,它不起作用,因為沒有方法以字母test開頭。 由於您使用的是 JUnit 4,只需將該類聲明為

public class EmailProviderTest {

並且可以從@Test注釋中找到測試方法。

閱讀: JUnit 混淆:使用“擴展測試用例”還是“@Test”?

我也得到了這個:

junit.framework.AssertionFailedError:在...中找不到測試

通過重命名從方法1測試方法來解決

@Test
public void method1(){
    // Do some stuff...
}

測試方法1

@Test
public void testMethod1(){
    // Do some stuff...
}

如果您使用的是 jUnit 4,則不要使用extends TestCase ,刪除此固定錯誤。

對於 Java 框架,我在 Intellij IDEA 中遇到了同樣的問題。 我做得很好:

  • 從 TestCase 繼承的類
  • 我已經導入了 junit.framework.TestCase
  • 我添加了裝飾@Test

但是我做錯了一件事:方法的名稱沒有以 "test" 開頭,實際上是:

@Test
public void getSomethingTest(){

當我把它改成:

@Test
public void testGetSomethingTest(){

我已經解決了:執行者終於能夠將此方法識別為測試方法。 我沒有改變其他任何東西。

我在創建private方法時遇到了JUnit 5的情況:

import org.junit.jupiter.api.Test

class Test1 {

    @Test
    private fun sort() {
        println("1")
    }
}

刪除private .

我在使用數據提供程序時使用了它,並且其中一個參數有一個換行符,如下所示:

@DataProvider
public static Object[][] invalidAdjustment() {
    return new Object[][]{
            {"some \n text", false},
            };
}

刪除\\n解決了問題

就我而言,我需要在任務junit的類路徑中使用junit4-version.jar並且還需要:

ant-version.jar
ant-junit-version.jar
ant-junit4-version.jar

在我的 ant 安裝庫 ( /usr/share/ant/lib ) 中。

我收到錯誤“ junit.framework.AssertionFailedError: No tests found in ... ”,而我沒有在正確的位置安裝ant-junit4-*version*.jar

我通過安裝ant-optional debian/ubuntu 包來糾正這個問題:

apt-get install ant-optional

我對此很煩惱,直到我將測試源文件從此文件夾中移出之前,所有答案都沒有幫助我:

src/test/java/com/junit/test

直到這個文件夾:

src/test/java/com/junit

另一個原因通常我看到人們有方法參數,刪除測試方法中可能有的任何參數,無論您添加 @Test 注釋,您都需要讓方法名稱以“test”開頭,以便 Junit 選擇您的測試用例。 希望能幫助到你。

測試方法名 public void test Name () {}

源命名方法中的name()在哪里。

暫無
暫無

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

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