2012-05-23 10 views
6

Ho una libreria di classi nunit contenente casi di test. Voglio ottenere a livello di programmazione un elenco di tutti i test nella libreria, principalmente i nomi dei test e i relativi ID di test. Ecco quello che ho finora:Ottieni una lista di test nella libreria nunit programmaticamente senza dover eseguire i test

var runner = new NUnit.Core.RemoteTestRunner(); 
runner.Load(new NUnit.Core.TestPackage(Request.PhysicalApplicationPath + "bin\\SystemTest.dll")); 
var tests = new List<NUnit.Core.TestResult>(); 
foreach (NUnit.Core.TestResult result in runner.TestResult.Results) 
{ 
    tests.Add(result); 
} 

Il problema è che runner.TestResult è nulla fino a quando effettivamente eseguire i test. Ovviamente non voglio eseguire i test a questo punto, voglio solo ottenere un elenco di quali test sono nella libreria. Successivamente, darò agli utenti la possibilità di selezionare un test ed eseguirlo individualmente, passando l'id di test all'istanza RemoteTestRunner.

Quindi, come posso ottenere l'elenco dei test senza effettivamente eseguirli tutti?

risposta

6

È possibile utilizzare la riflessione per caricare l'assieme e cercare tutti gli attributi test. Questo ti darebbe tutti i metodi che sono metodi di prova. Il resto sta a voi.

Ecco un esempio su msdn sull'utilizzo del reflection per ottenere gli attributi per un tipo. http://msdn.microsoft.com/en-us/library/z919e8tw.aspx

+1

+1, tuttavia v'è una torsione: con la TestCaseAttribute è possibile parametrizzare un metodo di prova, trasformando così in test multipli (logici). Nulla che non possa essere gestito dalla riflessione, ma qualcosa da tenere a mente. –

+0

@ Christian.K Buon punto, per l'OP da tenere a mente. –

+0

Inizialmente pensavo di farlo, dato che mi avrebbe dato i nomi di test (funzione), tuttavia non mi avrebbe dato gli ID di test. Finché riesco a eseguire un'esecuzione RemoteTestRunner filtrata dal nome del test anziché dall'id test, questo dovrebbe funzionare correttamente, lo verificherò. – Justin

4

Ecco il codice per recuperare tutti i nomi dei test fuori dal gruppo libreria di classe di test:

//load assembly. 
      var assembly = Assembly.LoadFile(Request.PhysicalApplicationPath + "bin\\SystemTest.dll"); 
      //get testfixture classes in assembly. 
      var testTypes = from t in assembly.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) 
       { 
        tests.Add(method.Name); 
       } 
      } 
1

La risposta da Justin non funziona per me. Quanto segue fa (recupera tutti i nomi di metodo con un attributo Test):

Assembly assembly = Assembly.LoadFrom("pathToDLL"); 
foreach (Type type in assembly.GetTypes()) 
{ 
    foreach (MethodInfo methodInfo in type.GetMethods()) 
    { 
     var attributes = methodInfo.GetCustomAttributes(true); 
     foreach (var attr in attributes) 
     { 
      if (attr.ToString() == "NUnit.Framework.TestAttribute") 
      { 
       var methodName = methodInfo.Name; 
       // Do stuff. 
      } 
     } 
    } 
} 
+0

Questo funziona. Se vuoi ottenere il conteggio dei test nel tuo attuale test in esecuzione è presente l'intera DLL aggiungere il seguente codice: if (TestContext.CurrentContext.Test.ClassName.Contains (type.Name)) {... –

Problemi correlati