2012-06-12 2 views
4

특정 시나리오에 따라 단위 테스트 클래스를 논리적 그룹으로 분할하려고합니다. 그러나 전체 테스트를 위해 실행되는 TestFixtureSetUpTestFixtureTearDown이 필요합니다. 기본적으로 나는 같은 것을 할 필요가 :NUnit을 사용하여 중첩 된 TestFixture 클래스 만들기

[TestFixture] 
class Tests { 
    private Foo _foo; // some disposable resource 

    [TestFixtureSetUp] 
    public void Setup() { 
     _foo = new Foo("VALUE"); 
    } 

    [TestFixture] 
    public class Given_some_scenario { 
     [Test] 
     public void foo_should_do_something_interesting() { 
      _foo.DoSomethingInteresting(); 
      Assert.IsTrue(_foo.DidSomethingInteresting); 
     } 
    } 

    [TestFixtureTearDown] 
    public void Teardown() { 
     _foo.Close(); // free up 
    } 
} 

을 나는 내부 클래스가 실행되기 전에 분해가 호출되고 아마도 때문에 _foo에있는 NullReferenceException를 얻을이 경우.

원하는 효과 (테스트 스코프)를 얻으려면 어떻게해야합니까? 확장 기능이나 도움이 될만한 NUnit을 사용할 수 있습니까? 차라리이 시간에 NUnit을 사용하고 SpecFlow와 같은 것을 사용하지 않을 것입니다.

답변

6

테스트를위한 추상 기본 클래스를 만들 수 있습니다. 거기에서 모든 설정 및 분해 작업을 수행 할 수 있습니다. 시나리오는 그 기본 클래스에서 상속됩니다.

[TestFixture] 
public abstract class TestBase { 
    protected Foo SystemUnderTest; 

    [Setup] 
    public void Setup() { 
     SystemUnterTest = new Foo("VALUE"); 
    } 

    [TearDown] 
    public void Teardown() { 
     SystemUnterTest.Close(); 
    } 
} 

public class Given_some_scenario : TestBase { 
    [Test] 
    public void foo_should_do_something_interesting() { 
     SystemUnderTest.DoSomethingInteresting(); 
     Assert.IsTrue(SystemUnterTest.DidSomethingInteresting); 
    } 
} 
+1

그러나'Given_some_scenario' 클래스에 다른 클래스를 중첩 할 수있는 방법이 없습니까? 아이디어는 포괄적 인 클래스를 전체 섹션 (예 :'CustomerTests ')과 관련시키고 각 시나리오 (예 :'When_searching_customers')에 대해 각각 하위 클래스로 만드는 것입니다. –

+0

상속을 사용하여 계층 구조를 그룹화하지 않는 이유는 무엇입니까? 'When_searching_customers : CustomerTestBase','When_creating_a_customer : CustomerTestBase' 등? –

+0

실제로 생각해 보면 실제로 작동 할 수 있습니다! 감사! –

관련 문제