przechowywać różne informacje na temat danego testu (identyfikatory dla wielu bugtracker) w atrybut tak:Jak uzyskać atrybuty metody testu jednostkowego w środowisku wykonawczym z testu NUnit?
[TestCaseVersion("001","B-8345","X543")]
public void TestSomethingOrOther()
W celu pobrania tej informacji w trakcie testu, napisałem poniższy kod:
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;
}
}
}
problem polega na tym, że podczas uruchamiania testów poprzez NUnit w trybie Release, metoda, która powinna mieć nazwę testu i te atrybuty otrzymuje brzmienie:
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)
UPDATE Dla każdego, kto jest zainteresowany, ja likwidacji implementacji kodu w następujący sposób (tak, że każda z wartości atrybutów można uzyskać dostępu, bez zmiany istniejącego kodu, który używa atrybutu TestCaseVersion:
[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"];
}
Niezła aktualizacja !!! Chciałbym dać ci drugi awans, gdybym mógł :-) –