Microsoft TestServer在TFS 2018构建任务上的NUnit / .Net Core中使请求失败

时间:2019-03-06 00:50:04

标签: c# asp.net-core nunit integration-testing tfsbuild

我已经使用.NET Core 2.2控制台应用程序和NUnit 3进行了一些API集成测试。API项目也使用.NET Core 2.2,并且一切都已配置,构建和运行正常。

但是,集成测试在构建到TFS 2018时无法正常工作。TFS构建中的dotnet test任务对于我的所有单元测试都可以正常工作,但是使用Microsoft.AspNetCore.TestHost.TestServer库编写的任何测试向API发出请求,失败并显示500

API控制器:

[Route("[controller]")]
public class AppController
{
    [HttpPost("bootstrap"), ActionName(nameof(GetApplicationBootstrap))]
    [ProducesResponseType(typeof(AppConfig), 200)]
    [ProducesResponseType(401)]
    public ActionResult<AppConfig> GetApplicationBootstrap()
    {
        return Ok(new AppConfig
        {
            IsAdministrator = true
        });
    }
}

集成测试:

public class TestFixtureBase
{
    internal HttpClient Client;
    internal TestServer Server;

    [SetUp]
    protected void SetUp()
    {
        Server = new TestServer(new WebHostBuilder().UseEnvironment("Development").UseStartup<TestServerStartup>());
        Client = Server.CreateClient();
    }

    [Test]
    public async Task Post_WhenCalled_ReturnsAppConfigModel_Ok()
    {
        //Arrange
        var request = new HttpRequestMessage(new HttpMethod("POST"), "/app/bootstrap");

        //Act
        var result = await Client.SendAsync(request);

        //Assert
        Assert.True(result.IsSuccessStatusCode);
    }

    [TestCase("GET")]
    [TestCase("PUT")]
    [TestCase("DELETE")]
    public async Task Get_WhenCalled_ReturnsAppConfigModel_Error(string method)
    {
        //Arrange
        var request = new HttpRequestMessage(new HttpMethod(method), "/app/bootstrap");

        //Act
        var result = await Client.SendAsync(request);

        //Assert
        Assert.False(result.IsSuccessStatusCode);
        Assert.AreEqual(HttpStatusCode.MethodNotAllowed, result.StatusCode);
    }

    [TearDown]
    protected void TearDown()
    {
        Server = null;
        Client = null;
    }
}

本地测试结果:

http://sqlfiddle.com/#!4/23656/5

Resharper results

在TFS上的测试结果:

dotnet test results Build task test

相同的测试在本地使用相同的命令dotnet test运行,对于在Build任务中运行的每个集成测试,我得到的是InternalServerError而不是MethodNotAllowed

我在构建服务器上缺少某些配置设置吗?代理的设置是否需要不同?我有VS2017 Enterprise,具有最新更新,并且IIS在服务器上全面打开。构建代理程序是否不能像本地工作站一样设置TestHost?我在本地运行dotnet test和运行相同命令的Build Agent有什么区别?

任何有关此谜的线索都将受到赞赏。

0 个答案:

没有答案