首页 文章

衡量框架例外

提问于
浏览
2

我开始使用Gauge Framework进行一些测试 . 我有一些验证方法,当测试的值无效时,返回一个名为FieldFunctionException的自定义异常 .

为了在Junit中测试,我使用了类似的东西:

@Test(expected = FieldFunctionException.class)
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException { 
... 
}

使用Gauge框架:

@Step("Scenario failing ")
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException {
...
}

但我没有找到如何通知Gauge这个抛出FieldFunctionException的测试是正常的行为 .

解决方法:

在我的情况下,我想测试异常 . 换句话说,异常抛出是一个成功的测试场景 . 我为测试异常所做的解决方法是这样的:

@Step("Scenario failing")
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException {
    AllowedValuesValidation f = new AllowedValuesValidation().allowedValues(new String[] { "Yes", "No", "Maybe" });
    try {
        String returned = f.execute("Maybes", null);
    }catch (Exception e) {
        assertTrue(e instanceof FieldFunctionException);
    }
}

1 回答

  • 1

    Gauge允许您使用 ContinueOnFailure 标记步骤实现,该实现通知框架您的测试代码可能抛出异常并且应该忽略它 .

    您可以指定要显式忽略的异常类型,例如 . 在你的情况下 FieldFunctionException.class .

    有关详细参考,请参阅this .

相关问题