【问题标题】:Gradle "test" task does not fail where it should beGradle“测试”任务不会在应有的地方失败
【发布时间】:2014-01-03 12:49:00
【问题描述】:

您好,我是 Gradle 新手。我们只是从 Maven 切换到 Gradle,我对 Gradle 测试任务的理解有些困难。 这是我的课:

public class Money   {
private final int amount;
private final String currency;

public Money(int amount, String currency) {
    if (amount < 0) {
        throw new IllegalArgumentException("illegal amount: [" + amount + "]");
    }
    if (currency == null || currency.isEmpty()) {
        throw new IllegalArgumentException("illegal currency: [" + currency + "]");
    }
    //this.amount = 15;
    this.amount = amount;
    this.currency = currency;
}

public int getAmount() {
    return amount;
}

public String getCurrency() {
    return currency;
}
}

这是我的测试(使用 TestNG):

@Test
public class MoneyManyValuesTest {
  public void testConstructor() {
    Money money = new Money(10, "USD");
    assertEquals(money.getAmount(), 10);
    assertEquals(money.getCurrency(), "USD");
    money = new Money(20, "EUR");
    assertEquals(money.getAmount(), 20);
    assertEquals(money.getCurrency(), "EUR");
 }
}

当我运行测试(使用 gradle "test" 任务)时,一切看起来都正常。

但是我希望我的测试失败,所以我像这样调整测试的最后一行:

assertEquals(money.getCurrency(), "EUR want error here");

再次运行 gradle "test" 测试仍然通过 msg:

2:40:26 PM: Executing external task 'test'...
money:compileJava UP-TO-DATE
money:processResources UP-TO-DATE
money:classes UP-TO-DATE
money:compileTestJava
money:processTestResources UP-TO-DATE
money:testClasses
money:test

BUILD SUCCESSFUL

这是怎么回事?

这是我的构建脚本

apply plugin: 'idea'
apply plugin: 'eclipse'

apply plugin: 'java';
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'org.testng:testng:6.3.1'
    testCompile 'org.mockito:mockito-all:1.9.0'
    testCompile 'org.easytesting:fest-assert:1.4'
    testCompile 'org.hamcrest:hamcrest-all:1.1'
}

test {
    useTestNG()
}
}

【问题讨论】:

  • 您是否指示 Gradle 使用 TestNG?您是否查看了完整 Gradle 发行版中的 Gradle User Guide(搜索 TestNG)和 TestNG 示例?您是否使用--info 或--debug 运行构建以获取更多信息?很可能构建没有执行您的测试。
  • 测试报告说什么(在 build/report/tests 中)?
  • 我的 gradle.build 包含测试 {useTestNG()}。不知何故,它没有看到我的代码发生变化,并认为一切都是最新的
  • 发布您的 build.gradle 文件。你在使用插件 java 吗?

标签: java unit-testing tdd gradle testng


【解决方案1】:

我正在使用 IntelliJ IDEA。我的 IDE 会自动保存我的源代码。我已经习惯了这个功能,所以我什至忘记了 IDEA 是为我做的。 看起来每当我运行 Gradle“测试”任务时,IDEA 都不会自动保存我的源代码。但如果我手动保存,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2019-11-12
    • 2020-11-10
    相关资源
    最近更新 更多