【发布时间】:2019-06-25 17:17:02
【问题描述】:
我正在尝试在本地的 azure 函数项目上运行集成测试。我的方法是使用进程以编程方式启动 azure 服务器。由于某种原因,这不起作用。
这就是我设置过程的方式:
private Process process;
private RestClient client;
[OneTimeSetUp]
public void OneTimeSetUp()
{
const string dotnetExePath = @"C:\Program Files\dotnet\dotnet.exe";
const string functionHostPath = @"C:\Users\MyName\AppData\Local\AzureFunctionsTools\Releases\2.24.0\cli\func.dll";
const string functionAppFolder = @"C:\Users\MyName\Source\Repos\ShyftApi\ShyftApi\bin\Debug\netstandard2.0";
process = new Process
{
StartInfo =
{
FileName = dotnetExePath,
Arguments = $"\"{functionHostPath}\" start -p {7001}",
WorkingDirectory = functionAppFolder
}
};
bool success = process.Start();
Assert.AreEqual(true, success);
client = new RestClient($"http://localhost:{7001}/api");
Thread.Sleep(8000);
}
OneTimeSetup 成功且没有错误,所以我假设进程正确启动(但我没有看到 azure shell,奇怪吗?)
我运行一个简单的 http 调用(使用 restsharp):
[Test]
public void SimpleTest()
{
RestRequest request = new RestRequest("/test", Method.POST);
IRestResponse response = client.Execute(request);
Assert.AreEqual(200, response.StatusCode);
}
这是我要测试的天蓝色函数:
[FunctionName("test")]
public static IActionResult Test([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequest req, TraceWriter log)
{
return new OkObjectResult("ok");
}
测试失败,因为response.StatusCode 为 0,所以我假设服务器没有启动。
我遵循https://blog.kloud.com.au/2018/11/08/integration-testing-precompiled-v2-azure-functions/ 中使用的格式作为进程的参数。我已手动检查路径是否正确(但该过程已成功启动,所以我认为路径不是问题?)。
我尝试访问的端点是http://localhost:7071/api/test,并且当定期运行 azure 服务器 (F5) 并通过我的浏览器访问它时,我得到 200 ok。
您认为可能是什么问题?进程的起始参数还是我只是使用了错误的 url?
【问题讨论】:
标签: azure rest process integration-testing restsharp