2012-04-19 3 views
1

구현과 관련없는 일종의 픽스처를 만들려고합니다.NUnit 테스트 픽스처 계층 구조

다음 인터페이스가 있습니다.

public interface ISearchAlgorithm 
{ 
    // methods 
} 

은 내가 그것을 작동 방법을 정확히 알고, 그래서 모든 파생 클래스에 대한 테스트의 동일한 집합을 실행하려면 :

public class RootSearchAlgorithmsTests 
{ 
    private readonly ISearchAlgorithm _searchAlgorithm; 

    public RootSearchAlgorithmsTests(ISearchAlgorithm algorithm) 
    { 
     _searchAlgorithm = algorithm; 
    } 

    [Test] 
    public void TestCosFound() 
    { 
     // arrange 
     // act with _searchAlgorithm 
     // assert 
    } 

    [Test] 
    public void TestCosNotFound() 
    { 
     // arrange 
     // act with _searchAlgorithm 
     // assert 
    } 
    // etc 

그럼 각 파생 클래스에 대해 다음과 비품을 만듭니다

[TestFixture] 
public class BinarySearchTests : RootSearchAlgorithmsTests 
{ 
    public BinarySearchTests(): base(new BinarySearchAlgorithm()) {} 
} 

[TestFixture] 
public class NewtonSearchTests : RootSearchAlgorithmsTests 
{ 
    public NewtonSearchTests(): base(new NewtonSearchAlgorithm()) {} 
} 

그것은 그 R 번호 테스트 주자를 모두 제외하고 잘 작동하고 NUnit과 GUI뿐만 아니라 기본 클래스 테스트를 표시하고 더 적절한 생성자가 없기 때문에 당연히 그들이 실패합니다.

[TestFixture]으로 표시되지 않은 이유는 무엇입니까? 나는 [Test] 속성을 가진 메소드 때문에 추측 할 수 있습니까?

어떻게하면 기본 클래스를 방지 할 수 있으며 결과에 메소드가 표시되지 않습니까?

답변

6

NUnit에 Generic Test Fixtures을 사용하면 원하는 것을 얻을 수 있습니다.

[TestFixture(typeof(Implementation1))] 
[TestFixture(typeof(Implementation2))] 
public class RootSearchAlgorithmsTests<T> where T : ISearchAlgorithm, new() 
{ 
    private readonly ISearchAlgorithm _searchAlgorithm; 

    [SetUp] 
    public void SetUp() 
    { 
     _searchAlgorithm = new T(); 
    } 

    [Test] 
    public void TestCosFound() 
    { 
     // arrange 
     // act with _searchAlgorithm 
     // assert 
    } 

    [Test] 
    public void TestCosNotFound() 
    { 
     // arrange 
     // act with _searchAlgorithm 
     // assert 
    } 
    // etc 
} 
+0

이 솔루션에는 빈 생성자 요구 사항의 단점이 있으며,이 코드 생성자는 내 코드의 생성자가 비어있는 경우에도 피하기를 희망합니다. 나는 어떤 종류의 Construct 함수를 전달할 수 있다고 생각합니다. 감사. – Grozz

+0

Generic Test Fixtures with Parameters를 사용하여 생성자에 매개 변수를 전달할 수 있다고 생각합니다. –