代码之家  ›  专栏  ›  技术社区  ›  Moerwald

NUnit:在setup方法中获取testname

  •  2
  • Moerwald  · 技术社区  · 8 年前

    在NUnit中,我可以通过 [Setup] public void Setup() . 是否可以通过 Setup 方法指定用于?

    1 回复  |  直到 8 年前
        1
  •  4
  •   Rob Prouse    8 年前

    在NUnit 3中。x、 只需使用 TestContext.CurrentContext.Test.Name Test 喜欢 MethodName FullName 取决于你需要什么。

    [TestFixture]
    public class TestNameInSetup
    {
        [SetUp]
        public void SetUp()
        {
            var testName = TestContext.CurrentContext.Test.Name;
            TestContext.WriteLine($"SetUp for {testName}");
        }
    
        [Test]
        public void NamedTest()
        {
            var testName = TestContext.CurrentContext.Test.Name;
            TestContext.WriteLine($"Running test {testName}");
        }
    }