简体   繁体   English

在 NUnit 中更改 TestName

[英]Change TestName in NUnit

I am trying to change the testcase name as below:我正在尝试更改测试用例名称,如下所示:

    public static string getTestName()
    {
        return testName;
    }

    [TestCase(TestName = nameof(getTestName))]
    public void MyTest()
    {
        
    }

This is updating my testcase name to "getTestName" instead of executing getTestName function and using it's return value.这是将我的测试用例名称更新为“getTestName”,而不是执行 getTestName function 并使用它的返回值。

What am I missing here?我在这里错过了什么?

According to Microsoft: 据微软称:

A nameof expression produces the name of a variable, type, or member as the string constant: nameof表达式将变量、类型或成员的名称生成为字符串常量:

So nameof(getTestName) is an expression that evaluates to the name of the getTestName method.所以nameof(getTestName)是一个计算结果为getTestName方法名称的表达式。 The result of this expression is the string "getTestName" .该表达式的结果是字符串"getTestName" This expression does NOT invoke the getTestName method, as you seem to think it should.该表达式不会调用getTestName方法,正如您认为它应该调用的那样。

You need to pass a constant to the TestCase attribute:您需要将常量传递给TestCase属性:

public class MyTests
{
    [TestCase(TestName = "One Option")]
    public void OneOption()
    {
    }

    private const string TestName = "Another Option";
    [TestCase(TestName = TestName)]
    public void AnotherOption()
    {
    }
    
    // This will evaluate to "MyTests", which is the name of the class passed to the nameof expression.
    [TestCase(TestName = nameof(MyTests))]
    public void YetAnotherOption()
    {
    }
}

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

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