.net 找不到 Assert.Fail 和 Assert.Pass 或等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14631923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:52:09  来源:igfitidea点击:

Cannot find Assert.Fail and Assert.Pass or equivalent

.netxunit.net

提问by naeron84

I used to use these in NUnit and they are really useful. Any idea how to do something like that?

我曾经在 NUnit 中使用过这些,它们非常有用。知道如何做这样的事情吗?

EDIT, CODE SAMPLE:

编辑,代码示例:

        bool condition = false;//would be nice not to have this
        observable.Subscribe(_ =>
        {
            if (real test)
                condition= true;//Assert.Pass()
        });
        StartObservable();
        Assert.True(condition);//Assert.Fail()      

回答by Jon Skeet

The documentation includes a comparison chartincluding this:

该文档包括一个比较图表,其中包括:

Fail- xUnit.net alternative: Assert.True(false, "message")

Fail- xUnit.net 替代方案: Assert.True(false, "message")

(It doesn't show Assert.Pass, and I've never used that myself, but I suspect the alternative is just to return from the test. Of course that doesn't help if you want to throw it in a nested method call. My suspicionis that it's not very frequently used in NUnit, hence its absence in the comparison chart.)

(它没有显示Assert.Pass,而且我自己从未使用过,但我怀疑另一种选择只是从测试中返回。当然,如果您想将其放入嵌套方法调用中,那将无济于事。我的怀疑是因为它在 NUnit 中不是很常用,因此它在比较图中没有出现。)

回答by Nik

An alternative to Assert.Fail("messsage")suggested by xUnit docs

xUnit 文档Assert.Fail("messsage")建议的替代方案

xUnit.net alternative: Assert.True(false, "message")

xUnit.net 替代方案:Assert.True(false, "message")

has a downside – its output is

有一个缺点——它的输出是

message

Expected: True
Actual:   False

To get rid of

摆脱

Expected: True
Actual:   False
Expected: True
Actual:   False

don't call Assert.True(false, "message")throw Xunit.Sdk.XunitExceptioninstead.
For example, create a helper method similar to this:

不要调用Assert.True(false, "message")throw Xunit.Sdk.XunitException
例如,创建一个类似于此的辅助方法:

public static class MyAssert
{
    public static void Fail(string message)
        => throw new Xunit.Sdk.XunitException(message);
}

回答by Tony Wall

Just throw an exception to satisfy both requirements (exiting a nested loop and an alternative to the missing Assert.Fail method). Only issue is there is no decent base exception (e.g. TestException) to use in order to avoid getting warnings about using the base Exception class, so something more directed like an InvalidOperationException is probably a good choice.

只需抛出一个异常即可满足这两个要求(退出嵌套循环和缺少 Assert.Fail 方法的替代方法)。唯一的问题是没有合适的基本异常(例如 TestException)来避免使用基本异常类的警告,所以像 InvalidOperationException 这样更直接的东西可能是一个不错的选择。