我想访问Nunit Author属性并在设置方法中获取作者值。请让我知道正确的做法。
下面是我尝试访问“作者”属性的方法,但返回空值。它给了我异常,对象引用未设置为对象的实例。
[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();
}
您正在获取'PropertyNames.Author'的空值,因为您试图在设置它之前访问Author属性。
因为设置是在每种Test方法之前执行的。您可以在'TearDown'方法中获取Author值,并在日志中使用它(假设您尝试将author值用于某些日志记录)。
您可以阅读有关Nunit属性here的更多信息>