ASP.NET WebApi 서비스에 대한 테스트를 작성하고 자체 호스팅 서비스 및 라이브 웹 호스팅 서비스에 대해 실행하고 싶습니다. 테스트 픽스처로이 작업을 수행 할 수 있다고 상상하지만, 어떻게 설정해야할지 모르겠습니다. 누구든지 구성 가능한 테스트 픽스처를 사용하여 Xunit에 매개 변수를 전달하여 자체 호스팅 된 조명기 또는 웹 호스팅 된 조명기를 선택할 수있는 예를 알고 있습니까?자체 호스트 및 프로덕션 webapi 서비스 모두에서 XUnit 테스트를 실행하는 방법은 무엇입니까?
1
A
답변
0
컨트롤러 테스트를 위해 인 메모리 서버를 사용하는 것이 좋으므로 단위 테스트에서 자체 호스트를 만들 필요가 없습니다. 이 최신 xUnit의 2.0 베타와 함께 작동 방법은 다음과
http://blogs.msdn.com/b/youssefm/archive/2013/01/28/writing-tests-for-an-asp-net-webapi-service.aspx
1
이다.
public class SelfHostFixture : IDisposable {
public static string HostBaseAddress { get; private set; }
HttpSelfHostServer server;
HttpSelfHostConfiguration config;
static SelfHostFixture() {
HostBaseAddress = ConfigurationManager.AppSettings["HostBaseAddress"]; // HttpClient in your tests will need to use same base address
if (!HostBaseAddress.EndsWith("/"))
HostBaseAddress += "/";
}
public SelfHostFixture() {
if (/*your condition to check if running against live*/) {
config = new HttpSelfHostConfiguration(HostBaseAddress);
WebApiConfig.Register(config); // init your web api application
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
}
public void Dispose() {
if (server != null) {
server.CloseAsync().Wait();
server.Dispose();
server = null;
config.Dispose();
config = null;
}
}
}
가 그런 다음 고정을 사용합니다 컬렉션을 정의
는 고정을 만듭니다. 컬렉션은 xUnit 2에서 테스트를 그룹화하는 새로운 개념입니다.[CollectionDefinition("SelfHostCollection")]
public class SelfHostCollection : ICollectionFixture<SelfHostFixture> {}
이는 구현이 없으므로 마커 역할을합니다. 지금, 당신의 호스트에 의존 마크 테스트는 수집 될 수 있습니다 :
[Collection("SelfHostCollection")]
public class MyController1Test {}
[Collection("SelfHostCollection")]
public class MyController4Test {}
서버가 한 번만 시작을 보장하여 MyController1Test
내에서 모든 테스트를 실행 치구와 MyController4Test
의 단일 인스턴스를 생성합니다 주자 컬렉션 당.
메모리 부족 테스트를 수행 할 때 우리는 요청과 응답이 형식 자의 직렬화/직렬화 과정을 거쳐 어떤 문제를 잡을 수 있는지 확인해야합니다. 내 아주 오래된 게시물의 일부 정보는 여기에 있습니다. http://blogs.msdn.com/b/kiranchalla/archive/2012/05/06/in-memory-client-amp-host-and-integration-testing-of-your-web-api-service.aspx. ..이 고려, 자기 호스트 테스트를하는 것이 더 나은 옵션이라고 생각 ... –