简体   繁体   English

NUnit 将变量从 [Test] 传递到 [TearDown]

[英]NUnit Pass variable from [Test] to [TearDown]

I need to pass the variable that contains Guid to the TearDown method.我需要将包含 Guid 的变量传递给 TearDown 方法。 Something like this:像这样的东西:

    [Test]
    public void MyTest()
    {
        ...
        var myVariable = $"Test-{Guid.NewGuid()}";
        ...
    }

So, I need to pass this 'myVariable' to the TearDown:所以,我需要将这个“myVariable”传递给 TearDown:

    [TearDown]
    public void TearDown()
    {
        ...
        DoSomethingWith(myVariable);
        ...
    }

I tried to set the Category for my Test, but it does not work with Guid Also I tried to figure out how to parse myVariable from TestContext.CurrentContext... obj, but wasn't success:(我试图为我的测试设置类别,但它不适用于 Guid 我还试图弄清楚如何从 TestContext.CurrentContext... obj 解析 myVariable,但没有成功:(

Make it a field instead of a variable.使其成为字段而不是变量。

class MyTest
{
    string? myField = null;

    [Test]
    public void MyTest()
    {
        ...
        myField = $"Test-{Guid.NewGuid()}";
        ...
    }

    [TearDown]
    public void TearDown()
    {
        ...
        DoSomethingWith(myField);
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM