Click here to Skip to main content
11,709,393 members (44,081 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: NUnit reflection
I'm trying to run NUNIT tests through Reflection in C#. I load the assemble and get the methods, then I want to filter out for methods only assigned the NUnit.FrameWork.TestAttribute.

I have tried this and it doesn't work:

var testTypes = from t in ltAssm.GetTypes()
                       let attributes = t.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute), true)
                       where attributes != null && attributes.Length > 0
                       orderby t.Name
                       select t;
       foreach (var type in testTypes)
       {
           //get test method in class.
           var testMethods = from m in type.GetMethods()
                             let attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true)
                             where attributes != null && attributes.Length > 0
                             orderby m.Name
                             select m;
           foreach (var method in testMethods)
           {
               methods.Add(method);
           }
       }
no methods picked up even though there are 50.

Even running this simple line doesn't pick anything up:

var xx = classMethods[11].GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), false);
I also tried instantiating the type and still nothing.

Those same methods have another custom attribute which I created and assigned to them, if I attempt to retrieve the methods using that Attrib, then they are retrieved.

Anyone know of something special that needs to be done for Reflection to recognize Nunit's TestAttribute?
Posted 12-Nov-12 3:13am
Comments
Sergey Alexandrovich Kryukov at 12-Nov-12 13:23pm
   
You did not show the code sample to be tested. How can we know that you correctly applied the test attributes to the test classes and test methods?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 OriginalGriff 250
2 Maciej Los 225
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,027
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100