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

时间:2010-01-03 08:45:23

标签: c# unit-testing nunit

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

有没有解决方案?

2 个答案:

答案 0 :(得分: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() { }
}

Nunit 不支持Assert.Inconclusive()中的TestFixtureSetUp。如果对Assert.Inconclusive()的调用完成,则夹具中的所有测试都显示为失败。

答案 1 :(得分:3)

试试这个:

  • TestFixtureSetUp中,在类中存储一个静态值,以指示数据是否尚未加载,是否已成功加载,或是否已尝试但未成功加载。

  • 在每个测试的SetUp中,检查值。

  • 如果它表示加载失败,请立即拨打Assert.Inconclusive()进行轰炸。

相关问题