C# 如何在nunit测试用例中使用Author属性

C# 如何在nunit测试用例中使用Author属性,c#,selenium,selenium-webdriver,nunit-3.0,C#,Selenium,Selenium Webdriver,Nunit 3.0,我想访问Nunit Author属性并在setup方法中获取Author值。 请告诉我正确的方法 下面是我尝试访问“Author”属性的方式,但得到的返回值为null。 它给了我一个异常,对象引用未设置为对象的实例 [TestFixture] public class EPTestFlow : MerlinBase { [Test] [Property(PropertyNames.Author,"Kalyani")] [TestC

我想访问Nunit Author属性并在setup方法中获取Author值。 请告诉我正确的方法

下面是我尝试访问“Author”属性的方式,但得到的返回值为null。 它给了我一个异常,对象引用未设置为对象的实例

[TestFixture]
    public class EPTestFlow : MerlinBase
    {

        [Test]
        [Property(PropertyNames.Author,"Kalyani")]
        [TestCaseSource(typeof(TestDataMerlin), "LoginDetails", new object[] { new string[] { "TC01"} })]
        public void PatientEnrollment(string userDetails, LoginDetails loginDetails)
        {

        }
     }

更新了建议的方法:

        [Test]
        [Author("Kalyani")]
        [TestCaseSource(typeof(TestDataMerlin), "LoginDetails", new object[] { new string[] { "TC01"} })]
        public void PatientEnrollment(string userDetails, LoginDetails loginDetails)
        {
        }

“PropertyNames.Author”的值为空,因为您试图在设置它之前访问Author属性

由于安装是在每个测试方法之前执行的,所以在安装方法中Author的值为null。您可以在“TearDown”方法中获取Author值,并在日志中使用它(假设您尝试在某些日志中使用Author值)

您可以阅读有关Nunit属性的更多信息

尝试在测试方法中设置author属性,如下所示

 [Test]
 [Author("Author_Name")]
 public void TestTest() { /* ... */ }
}

        [Test]        
        [TestCaseSource(typeof(TestDataMerlin), "LoginDetails", new object[] { new string[] { "TC01"} })]
        public void PatientEnrollment(string userDetails, LoginDetails loginDetails)
        {                      
        TestExecutionContext.CurrentContext.CurrentTest.Properties.Add("Author", "Author_Name");
        }

        [TearDown]
        public void TestCleanup()
        {
            string name = TestExecutionContext.CurrentContext.CurrentTest.Properties.Get("Author").ToString();
        }
并在拆卸方法中使用下面的方法进行检索

var category = (string) TestContext.CurrentContext.Test.Properties.Get("Author");
如果您使用的是TestCaseSource属性,那么使用下面的设置测试方法中的Author属性

 [Test]
 [Author("Author_Name")]
 public void TestTest() { /* ... */ }
}

        [Test]        
        [TestCaseSource(typeof(TestDataMerlin), "LoginDetails", new object[] { new string[] { "TC01"} })]
        public void PatientEnrollment(string userDetails, LoginDetails loginDetails)
        {                      
        TestExecutionContext.CurrentContext.CurrentTest.Properties.Add("Author", "Author_Name");
        }

        [TearDown]
        public void TestCleanup()
        {
            string name = TestExecutionContext.CurrentContext.CurrentTest.Properties.Get("Author").ToString();
        }

我尝试在teardown中访问author属性,但现在它返回0值。[teardown]public void TestCleanup(){IList testAuthor=(IList)TestContext.CurrentContext.Test.Properties[“author”];foreach(testAuthor中的字符串作者){string name12=author.ToString()}根据注释编辑了答案。请检查。我尝试使用您提供的解决方案,但仍然得到空值。由于我使用的是TestCaseSource属性,因此认为此属性是否会导致问题。请使用“使用建议的方法更新”检查上述问题: