从 mstest.exe 启动 OWIN TestServer
Start OWIN TestServer from mstest.exe
我有一个工作单元测试,用于设置 TestServer、创建请求并验证响应。
当我 运行 来自 Visual Studio Test Explorer 的那些单元测试时,它们运行良好。已设置测试服务器,发出请求并返回正确的响应。
当我 运行 来自命令行的相同代码(在我们的 CI 服务器上)时,TestServer 总是 returns 一个 "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 内运行良好。并且所有其他单元测试都按预期从命令行执行。
非常感谢!
我找到了解决问题的方法。 NotFound
错误出现是因为我想测试的 OAuth 配置在 运行 作为单元测试时将 AllowInsecureHttp
属性 设置为 false
。我确保 AllowInsecureHttp
不仅在 DEBUG
模式下而且在单元测试为 运行.
时也是 true
现在完美运行了。
我有一个工作单元测试,用于设置 TestServer、创建请求并验证响应。
当我 运行 来自 Visual Studio Test Explorer 的那些单元测试时,它们运行良好。已设置测试服务器,发出请求并返回正确的响应。
当我 运行 来自命令行的相同代码(在我们的 CI 服务器上)时,TestServer 总是 returns 一个 "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 内运行良好。并且所有其他单元测试都按预期从命令行执行。
非常感谢!
我找到了解决问题的方法。 NotFound
错误出现是因为我想测试的 OAuth 配置在 运行 作为单元测试时将 AllowInsecureHttp
属性 设置为 false
。我确保 AllowInsecureHttp
不仅在 DEBUG
模式下而且在单元测试为 运行.
true
现在完美运行了。