2013-02-22 8 views
8

C'è un modo per aggiungere argomenti ad un metodo di installazione nunit come questo: public void SetUp(Point p = null) { /*code*/ }.C'è un modo per creare nunit Setup con argomenti

ho provato ed ho ottenuto la seguente eccezione SetUp : System.Reflection.TargetParameterCountException : Parameter count mismatch

+1

Per quanto come so, non è possibile utilizzare gli argomenti nel metodo '[Setup]'. Tuttavia, forse puoi beneficiare di * NUnit * [attributi azione] (http://nunit.com/index.php?p=actionAttributes&r=2.6.2)? –

+0

Il test runner NUnit non passa gli argomenti al metodo 'SetUp'. Per cosa ti serve? –

risposta

1

Credo che il punto è quello di evitare la duplicazione del codice. Prova ad estrarre la classe base con il metodo overriten usato in SetUp(). Tutti classe derivata eseguirà test dalla classe base, con oggetti preparati in OnSetUp overriten()

[TestFixture] 
public class BaseTestsClass 
{ 
    //some public/protected fields to be set in SetUp and OnSetUp 

    [SetUp] 
    public void SetUp() 
    { 
     //basic SetUp method 
     OnSetUp(); 
    } 

    public virtual void OnSetUp() 
    { 
    } 

    [Test] 
    public void SomeTestCase() 
    { 
     //... 
    } 

    [Test] 
    public void SomeOtherTestCase() 
    { 
     //... 
    } 
} 

[TestFixture] 
public class TestClassWithSpecificSetUp : BaseTestsClass 
{ 
    public virtual void OnSetUp() 
    { 
     //setup some fields 
    } 
} 

[TestFixture] 
public class OtherTestClassWithSpecificSetUp : BaseTestsClass 
{ 
    public virtual void OnSetUp() 
    { 
     //setup some fields 
    } 
} 

Uso TestFixture parametrizzata può anche essere utile. Le prove in classe verranno pronte per TestFixture, anche il metodo SetUp. Ma ricordate che

infissi con parametri sono (come avete scoperto) limitati dal fatto che è possibile utilizzare solo gli argomenti che sono consentiti negli attributi

Usage:

[TestFixture("some param", 123)] 
[TestFixture("another param", 456)] 
public class SomeTestsClass 
{ 
    private readonly string _firstParam; 
    private readonly int _secondParam; 

    public WhenNoFunctionCodeExpected(string firstParam, int secondParam) 
    { 
     _firstParam = firstParam; 
     _secondParam = secondParam; 
    } 

    [Test] 
    public void SomeTestCase() 
    { 
     ... 
    } 
} 
Problemi correlati