0

如果我做了一个应该抛出致命错误的测试,我该如何处理?例如,如何编写此测试以确保正确删除变量:

[Test]
public static void TestScope()
{
    String str;
    {
        str = scope .();
    }
    str.ToUpper(); // str should be marked as deleted here
}
4

1 回答 1

0

您可以将Test属性参数化为Test(ShouldFail=true).

测试过程首先运行所有不应该失败的测试,然后运行所有应该失败的测试。如果任何应该失败的测试没有,剩余的应该失败的测试仍然运行。

例如,测试这个类:

class Program
{
    [Test(ShouldFail=true)]
    public static void TestScopeShouldFailButSucceeds()
    {
        String str;
        {
        str = scope:: .();
        }

        str.ToUpper(); // will not fail
    }

    [Test(ShouldFail=true)]
    public static void TestScopeShouldFail()
    {
        String str;
        {
        str = scope .();
        }

        str.ToUpper(); // will fail
    }

    [Test]
    public static void TestScopeShouldNotFail()
    {
        String str;
        {
        str = scope:: .();
        }

        str.ToUpper(); // will not fail
    }

    public static void Main()
    {

    }

}

...将首先成功完成TestScopeShouldNotFail,然后将意外完成TestScopeShouldFailButSucceeds,然后将预期失败TestScopeShouldFail。因此,它将产生一个失败的测试TestScopeShouldFailButSucceeds

于 2020-01-18T05:57:33.130 回答