Assert.Fail() 被认为是不好的做法吗? [英] Is Assert.Fail() considered bad practice?

查看:47
本文介绍了Assert.Fail() 被认为是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做 TDD 时经常使用 Assert.Fail.我通常一次只做一个测试,但是当我对以后想要实现的事情有了想法时,我会快速编写一个空测试,其中测试方法的名称指示我想要实现的作为待办事项列表的内容.为了确保我不会忘记我在正文中放入了一个 Assert.Fail().

I use Assert.Fail a lot when doing TDD. I'm usually working on one test at a time but when I get ideas for things I want to implement later I quickly write an empty test where the name of the test method indicates what I want to implement as sort of a todo-list. To make sure I don't forget I put an Assert.Fail() in the body.

在尝试 xUnit.Net 时,我发现他们没有实现 Assert.Fail.当然,您始终可以 Assert.IsTrue(false) 但这也不能传达我的意图.我的印象是 Assert.Fail 不是故意实施的.这被认为是不好的做法吗?如果是为什么?

When trying out xUnit.Net I found they hadn't implemented Assert.Fail. Of course you can always Assert.IsTrue(false) but this doesn't communicate my intention as well. I got the impression Assert.Fail wasn't implemented on purpose. Is this considered bad practice? If so why?

@马丁梅雷迪思这不完全是我所做的.我确实先写了一个测试,然后实现代码使其工作.通常我会同时想到几个测试.或者当我在做其他事情时,我会考虑编写一个测试.那是我写一个空的失败测试来记住的时候.当我开始编写测试时,我会巧妙地以测试为先.

@Martin Meredith That's not exactly what I do. I do write a test first and then implement code to make it work. Usually I think of several tests at once. Or I think about a test to write when I'm working on something else. That's when I write an empty failing test to remember. By the time I get to writing the test I neatly work test-first.

@吉米这看起来是个好主意.忽略的测试不会失败,但它们仍会显示在单独的列表中.必须尝试一下.

@Jimmeh That looks like a good idea. Ignored tests don't fail but they still show up in a separate list. Have to try that out.

@马特豪威尔斯好点子.在这种情况下,NotImplementedException 比 assert.Fail() 更能传达意图

@Matt Howells Great Idea. NotImplementedException communicates intention better than assert.Fail() in this case

@米奇小麦这就是我一直在寻找的.它似乎被排除在外,以防止它以我滥用的另一种方式被滥用.

@Mitch Wheat That's what I was looking for. It seems it was left out to prevent it being abused in another way I abuse it.

推荐答案

对于这种情况,我没有调用 Assert.Fail,而是执行以下操作(在 C#/NUnit 中)

For this scenario, rather than calling Assert.Fail, I do the following (in C# / NUnit)

[Test]
public void MyClassDoesSomething()
{
    throw new NotImplementedException();
}

它比 Assert.Fail 更明确.

It is more explicit than an Assert.Fail.

似乎普遍同意使用比 Assert.Fail() 更明确的断言.大多数框架都必须包含它,因为它们没有提供更好的替代方案.例如,NUnit(和其他)提供了一个 ExpectedExceptionAttribute 来测试一些代码是否抛出了一个特定的异常类.但是,为了测试异常的属性是否设置为特定值,不能使用它.相反,您必须求助于 Assert.Fail:

There seems to be general agreement that it is preferable to use more explicit assertions than Assert.Fail(). Most frameworks have to include it though because they don't offer a better alternative. For example, NUnit (and others) provide an ExpectedExceptionAttribute to test that some code throws a particular class of exception. However in order to test that a property on the exception is set to a particular value, one cannot use it. Instead you have to resort to Assert.Fail:

[Test]
public void ThrowsExceptionCorrectly()
{
    const string BAD_INPUT = "bad input";
    try
    {
        new MyClass().DoSomething(BAD_INPUT);
        Assert.Fail("No exception was thrown");
    }
    catch (MyCustomException ex)
    {
         Assert.AreEqual(BAD_INPUT, ex.InputString); 
    }
}

xUnit.Net 方法 Assert.Throws 使这更简洁,而不需要 Assert.Fail 方法.通过不包含 Assert.Fail() 方法,xUnit.Net 鼓励开发人员寻找和使用更明确的替代方案,并在必要时支持创建新断言.

The xUnit.Net method Assert.Throws makes this a lot neater without requiring an Assert.Fail method. By not including an Assert.Fail() method xUnit.Net encourages developers to find and use more explicit alternatives, and to support the creation of new assertions where necessary.

这篇关于Assert.Fail() 被认为是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆