tôi lưu trữ thông tin khác nhau về việc xét nghiệm nhất định (ID cho nhiều hệ thống theo dõi bug) trong một thuộc tính như vậy:Làm thế nào để có được các thuộc tính phương pháp thử nghiệm đơn vị khi chạy trong một thử nghiệm NUnit?
[TestCaseVersion("001","B-8345","X543")]
public void TestSomethingOrOther()
Để lấy thông tin này trong quá trình thử nghiệm, tôi đã viết đoạn code dưới đây:
public string GetTestID()
{
StackTrace st = new StackTrace(1);
StackFrame sf;
for (int i = 1; i <= st.FrameCount; i++)
{
sf = st.GetFrame(i);
if (null == sf) continue;
MethodBase method = sf.GetMethod();
if (method.GetCustomAttributes(typeof(TestAttribute), true).Length == 1)
{
if (method.GetCustomAttributes(typeof(TestCaseVersion), true).Length == 1)
{
TestCaseVersion tcv =
sf.GetMethod().GetCustomAttributes(typeof(TestCaseVersion), true).OfType<TestCaseVersion>()
.First();
return tcv.TestID;
}
}
}
vấn đề là khi chạy thử nghiệm qua NUnit trong chế độ phát hành, phương pháp mà nên có tên thử nghiệm và những thuộc tính này được thay thế bởi những điều sau đây:
System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at NUnit.Core.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
CẬP NHẬT Đối với bất cứ ai quan tâm, tôi lên vết thương triển khai mã theo cách sau (vì vậy mà bất kỳ giá trị thuộc tính có thể được truy cập, mà không thay đổi bất kỳ mã hiện có sử dụng TestCaseVersion thuộc tính:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = false)]
public class TestCaseVersion : PropertyAttribute
{
public TestCaseVersion(string testCaseCode, string story, string task, string description)
{
base.Properties.Add("TestId", testCaseCode);
base.Properties.Add("Description", description);
base.Properties.Add("StoryId", story);
base.Properties.Add("TaskId", task);
}
}
public string GetTestID()
{
return TestContext.CurrentContext.Test.Properties["TestId"];
}
Cập nhật tốt đẹp !!! Tôi sẽ cung cấp cho bạn một upvote thứ hai nếu tôi có thể :-) –