简体   繁体   English

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

[英]how to use Author attribute in nunit test cases

I want to access Nunit Author attribute and fetch author value in setup method.我想访问 Nunit Author 属性并在 setup 方法中获取作者值。 Please let me know the correct way of doing it.请让我知道正确的做法。

Below is the way i tried accessing "Author" attribute,but getting null value in return.以下是我尝试访问“作者”属性的方式,但得到 null 值作为回报。 It is giving me exception Object reference not set to instance of object.它给了我异常 Object 参考未设置为 object 的实例。

[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)
        {

        }
     }

        [SetUp]
        public void TestInitialize()
        {
           var testAuthor = TestContext.CurrentContext.Test.Properties[PropertyNames.Author];
            string name = testAuthor.ToString();
        }

Updated with suggested approach:更新了建议的方法:

        [Test]
        [Author("Kalyani")]
        [TestCaseSource(typeof(TestDataMerlin), "LoginDetails", new object[] { new string[] { "TC01"} })]
        public void PatientEnrollment(string userDetails, LoginDetails loginDetails)
        {
        }
[TearDown]
        public void TestCleanup()
        {
            //IList testAuthor = (IList)TestContext.CurrentContext.Test.Properties["Author"];
            //foreach (string author in testAuthor)
            //{
            //    string name12 = author.ToString();
            //}
            //string name = testAuthor.ToString();

            var category = (string)TestContext.CurrentContext.Test.Properties.Get("Author");
}

You are getting null value for 'PropertyNames.Author' because you are trying to access the Author property before setting it.您正在获得“PropertyNames.Author”的 null 值,因为您在设置之前尝试访问 Author 属性。

Since SetUp is executed before each Test method the value of Author is null in SetUp method.由于 SetUp 在每个 Test 方法之前执行,因此在 SetUp 方法中 Author 的值是 null。 You can get the Author value in the 'TearDown' method and use it in the logs(Assuming you are trying to use the author value for some logging).您可以在“TearDown”方法中获取作者值并在日志中使用它(假设您尝试将作者值用于某些日志记录)。

You can read more about the Nunit attributes here您可以在此处阅读有关 Nunit 属性的更多信息

Try setting author attribute like below in Test method尝试在测试方法中设置作者属性,如下所示

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

and retrieve using below in TearDown method并在 TearDown 方法中使用以下检索

var category = (string) TestContext.CurrentContext.Test.Properties.Get("Author");

If you are using TestCaseSource attribute then use the below to set the Author property in Test method如果您使用的是 TestCaseSource 属性,则使用以下内容在 Test 方法中设置 Author 属性

        [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();
        }

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

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