2016-10-03 8 views
3

다중 배치 모드를 지원하는 프로젝트가 있습니다 : InMem, OnPremise, Cloud. 또한 각 프로젝트에는 TimeDistance와 같은 작은 서비스가 있습니다.이 서비스는 WCF 또는 API 중 하나에 연결될 수 있습니다. UnitTests를 다른 모형 세트로 두 번 실행하는 방법

을 unitTestMockup에서 내가 사용하고자하는 하나 말할 수있다 : APITimeDistance로 이동

Service.TimeDistance = new WCFTimeDistance()/new APITimeDistance(). 

은 지금까지 난 단지 WCFTimeDistance 있었지만 지금 우리는 전환 모드에 있지만 그 동안 내가 할 때 원하는 테스트를 두 번 실행합니다. 한 번만 WCF를 사용하여 API를 실행합니다.

어떻게하면 좋은 방법일까요?

I use C# 4.5 
Microsoft.VisualStudio.QualityTools.UnitTestFramework as framework for unitTests 

원하는 흐름의 간단한 예는이 될 것이다 :

1)Mockup: Service.TimeDistance = new WCFTimeDistance(); 
2)UnitTest: CheckDistanceBetweenTwoLocationsTest() 
{ 
Service.TimeDistance.CalculateDistance(Location1, Location2) // WCFTimeDistance 
} 
3)Mockup: Service.TimeDistance = new APITimeDistance(); 
UnitTest: CheckDistanceBetweenTwoLocationsTest() 
{ 
4)Service.TimeDistance.CalculateDistance(Location1, Location2) // APITimeDistance 
} 
+1

죄송합니다 - 어떤 언어, 어떤 단위 테스트 프레임 워크? 또한 [mcve]를 제공 할 수 있습니까? 그래도 큰 질문입니다! – Mafii

+1

수정 됨, 감사합니다! –

+0

@AndreiDutu 두 개의 단위 테스트를 만듭니다. 또한 정적 클래스 대신 추상화를 사용하십시오. 조롱과 테스트가 더 쉬워 질 것입니다. – Nkosi

답변

3

두 단위 테스트를 만든다. 또한 정적 클래스 대신 추상화를 사용하십시오. 조롱과 테스트가 더 쉬울 것입니다.

이상적으로는 추상적 인 서비스에 원하는 것이 현재 설정

[TestClass] 
public class TimeDistanceTests { 
    //change these to the needed types 
    object Location1; 
    object Location2; 
    double expected; 

    [TestInitialize] 
    public void Init() { 
     //...setup the two locations and expectations 
     Location1 = //... 
     Location2 = //... 
     expected = //...the distance. 
    } 

    [TestMethod] 
    public void Check_Distance_Between_Two_Locations_Using_WCF() { 
     //Arrange 
     var sut = new WCFTimeDistance(); 
     //Act 
     var actual = sut.CalculateDistance(Location1, Location2); 
     //Assert 
     //...some assertion proving that test passes or failed 
     Assert.AreEqual(expected, actual); 
    } 

    [TestMethod] 
    public void Check_Distance_Between_Two_Locations_Using_API() { 
     //Arrange 
     var sut = new APITimeDistance(); 
     //Act 
     var actual = sut.CalculateDistance(Location1, Location2); 
     //Assert 
     //...some assertion proving that test passes or failed 
     Assert.AreEqual(expected, actual); 
    } 
} 

입니다.

public interface ITimeDistance { 
    double CalculateDistance(Location location1, Location location2); 
} 

귀하의 서비스 같은 것을 가정하면 대신 결핵의 추상화를 사용합니다.

public class Service { 
    ITimeDistance timeDistance 
    public Service(ITimeDistance timeDistance) { 
     this.timeDistance = timeDistance; 
    } 

    public ITimeDistance TimeDistance { get { return timeDistance; } } 
} 

이제 단위 테스트에서 서로 다른 시간 서비스를 구현할 수 있습니다.

+0

이것은 정확히 서비스가 추상화 된 방법입니다. 나는 이미 ~ 110 UnitTests를 가지고 너무 많은 중복 코드가 될 것이므로 중복 테스트를 피한다. 그게 내가 지금하고있는 일이다. 나는 새로운 열거 형을 만들었다 : WCF, API 그리고 나는 WCF에 대한 단위 테스트를 먼저 실행했다. 이 모든 것을 자동으로 만들 수있는 방법이 있다고 생각했습니다. –

+1

@AndreiDutu는 NUnit이나 xUnit과 같은 단위 테스팅 프레임 워크를 살펴 봅니다. 여기를보십시오 : http://nunit.org/?p=testCase&r=2.5 – Mafii