Testing 如何在自主机和;生产webapi服务?

Testing 如何在自主机和;生产webapi服务?,testing,asp.net-web-api,xunit,fixture,self-hosting,Testing,Asp.net Web Api,Xunit,Fixture,Self Hosting,我想为我的ASP.NETWebAPI服务编写一个测试,并针对自托管服务和实时web托管服务运行它。我想这可以通过测试夹具来完成,但我不确定如何设置它。是否有人知道使用可配置测试设备的示例,以便您可以将参数传递给Xunit以选择自托管设备或web托管设备?我建议使用内存服务器来测试控制器,这样您就不需要在单元测试中启动自托管设备 我建议使用内存服务器来测试控制器,这样您就不需要在单元测试中启动自主机 以下是它如何与最新的xUnit 2.0测试版配合使用 创建设备: public class Sel

我想为我的ASP.NETWebAPI服务编写一个测试,并针对自托管服务和实时web托管服务运行它。我想这可以通过测试夹具来完成,但我不确定如何设置它。是否有人知道使用可配置测试设备的示例,以便您可以将参数传递给Xunit以选择自托管设备或web托管设备?

我建议使用内存服务器来测试控制器,这样您就不需要在单元测试中启动自托管设备


我建议使用内存服务器来测试控制器,这样您就不需要在单元测试中启动自主机


以下是它如何与最新的xUnit 2.0测试版配合使用

创建设备:

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;
        }
    }
}
然后定义将使用该装置的集合。集合是XUnit2中分组测试的新概念

[CollectionDefinition("SelfHostCollection")]
public class SelfHostCollection : ICollectionFixture<SelfHostFixture> {}

当从
MyController1Test
MyController4Test
运行任何测试时,运行程序将创建一个夹具实例,以确保每个集合只启动一次服务器。

以下是它如何与最新的xUnit 2.0测试版配合使用

创建设备:

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;
        }
    }
}
然后定义将使用该装置的集合。集合是XUnit2中分组测试的新概念

[CollectionDefinition("SelfHostCollection")]
public class SelfHostCollection : ICollectionFixture<SelfHostFixture> {}

当运行
MyController1Test
MyController4Test
中的任何测试时,运行程序将创建设备的单个实例,以确保每个集合只启动一次服务器。

仅供参考…执行内存内测试时,我们需要确保请求和响应通过格式化程序的序列化/反序列化过程来捕获任何问题…我的老帖子中有一些信息:。。。考虑到这一点,我认为进行自主机测试是一个更好的选择…仅供参考…在进行内存测试时,我们需要确保请求和响应通过格式化程序的序列化/反序列化过程来捕获任何问题…我的老帖子中有一些信息:。。。考虑到这一点,我认为做自我主机测试是一个更好的选择。。。