Start OWIN TestServer from mstest.exe

本文关键字:mstest exe from TestServer OWIN Start | 更新日期: 2023-09-27 18:12:54

我有一个工作单元测试,它设置了一个TestServer,创建请求并验证响应。

当我从Visual Studio Test Explorer中运行这些单元测试时,它们工作得很好。设置TestServer,发出请求并返回正确的响应。

当我从命令行(在我们的CI服务器上)运行相同的代码时,TestServer总是返回"Not Found"的状态代码。没有其他例外。从命令行启动OWIN时,我需要添加任何特定的命令吗?

MSTest.exe命令如下所示:

MSTest.exe /testcontainer:myTests.dll /resultsfile:testresults.trx

我的测试方法是这样的:

[TestMethod]
public async Task GetToken()
{
   using (var server = TestServer.Create<TestStartupConfiguration>())
   {
      var response = await server.CreateRequest("/TokenUrl").And(x => x.Content = new FormUrlEncodedContent(new[]
       {
           new KeyValuePair<string, string>("username", user.UserName),
           new KeyValuePair<string, string>("password", user.Password),
           new KeyValuePair<string, string>("grant_type", "password")
        })).PostAsync();
       response.IsSuccessStatusCode.Should().BeTrue("this is the expected return value of an Access Token request");
       var responseString = await response.Content.ReadAsStringAsync();
       responseString.Should().NotBeNullOrWhiteSpace("the response of an Access Token request should not be empty");
    }
}

并且,正如我所说的,在Visual Studio中测试工作得很好。并且所有其他单元测试都按照预期从命令行执行。

非常感谢!

Start OWIN TestServer from mstest.exe

我找到了解决问题的方法。NotFound错误是因为我想要测试的OAuth配置在作为单元测试运行时将AllowInsecureHttp属性设置为false。我确保AllowInsecureHttptrue不仅在DEBUG模式,而且当一个单元测试正在运行。