如何将整个班级标记为"不确定"?

Afs*_*bbi 3 c# nunit unit-testing

我有一个名为MyClass的测试类.MyClass有一个TestFixtureSetUp,可以加载一些初始数据.我想在加载初始数据失败时将整个类标记为不确定.就像有人通过调用Assert.Inconclusive()标记一个测试方法Inconclusive.

有什么解决方案吗?

Eli*_*sha 6

您可以Setup通过在数据加载失败时发出信号来解决它.

例如:

[TestFixture]
public class ClassWithDataLoad
{
    private bool loadFailed;

    [TestFixtureSetUp]
    public void FixtureSetup()
    {
        // Assuming loading failure throws exception.
        // If not if-else can be used.
        try 
        {
            // Try load data
        }
        catch (Exception)
        {
            loadFailed = true;
        }
    }

    [SetUp]
    public void Setup()
    {
        if (loadFailed)
        {
            Assert.Inconclusive();
        }
    }

    [Test] public void Test1() { }        
    [Test] public void Test2() { }
}
Run Code Online (Sandbox Code Playgroud)

NUnit的不支持Assert.Inconclusive()TestFixtureSetUp.如果进行了调用,Assert.Inconclusive()则夹具中的所有测试都显示为失败.