簡體   English   中英

JUnit 0 發現測試

[英]JUnit 0 Tests found

我想寫一個簡單的Testclass,在其中添加兩個數字:

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class ttest {

  private final ttest tT = new ttest();

  public int add(int one, int two) {
    return one + two;
  }

  @Test
  public void eq() {
    int result = tT.add(1, 2);
    assertEquals(result, 3);
    assertTrue(result == 3);
  }
}

它編譯正確,但如果我在控制台中運行測試,它會說:

├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
[         2 containers found      ]
[         2 containers successful ]
[         0 tests found           ]

它找到了所有“容器”,但沒有找到要添加數字的測試用例。 (我使用課程中的腳本開始測試運行,因此問題必須在.java中)

我在控制台中運行它的腳本是:

javac --class-path="junit-platform-console-standalone-1.6.2.jar" ttest*.java
java -jar junit-platform-console-standalone-1.6.2.jar --class-path="." --scan-class-path

例如,可以通過將 class 重命名為TTest來運行您的示例。

根據官方文檔: https://junit.org/junit5/docs/5.0.0-M5/user-guide/#running-tests-console-launcher

-n, --include-classname 提供正則表達式以僅包含完全限定名稱匹配的類。 為避免不必要地加載類,默認模式僅包含以“Test”或“Tests”結尾的 class 名稱。 重復此選項時,將使用 OR 語義組合所有模式。 (默認值:^.*Tests?$)

因此,只需將您的 class 與默認模式不匹配,因此不會被框架拾取。

我不知道這是否是您可以接受的解決方案。 使用以下代碼,它對我有用:

import org.junit.Assert;
import org.junit.Test;

public class ttest {

    //private final ttest tT = new ttest(); 

    public int add(int one, int two) {
        return one + two;
    }

    @Test
    public void eq() {
        int result = add(1, 2); 
        Assert.assertEquals(result, 3);
        Assert.assertTrue(result == 3);
    }
}

暫無
暫無

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

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