Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure功能本地集成测试_Azure_Rest_Process_Integration Testing_Restsharp - Fatal编程技术网

Azure功能本地集成测试

Azure功能本地集成测试,azure,rest,process,integration-testing,restsharp,Azure,Rest,Process,Integration Testing,Restsharp,我正在本地尝试在azure functions项目上运行集成测试。我的方法是使用进程以编程方式启动azure服务器。由于某些原因,这不起作用 以下是我设置流程的方式: private Process process; private RestClient client; [OneTimeSetUp] public void OneTimeSetUp() { const string dotnetExePath = @"C:\Program Files\dotnet\dotnet.exe

我正在本地尝试在azure functions项目上运行集成测试。我的方法是使用进程以编程方式启动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,wierd?)

我运行了一个简单的http调用(使用restsharp):

这是我正在尝试测试的azure函数:

[FunctionName("test")]
public static IActionResult Test([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequest req, TraceWriter log)
{
    return new OkObjectResult("ok");
}
测试失败是因为
response.StatusCode
为0,因此我假设服务器未启动

我遵循流程参数中使用的格式。我已经手动检查了路径是否正确(但是进程成功启动,所以我认为路径不是问题所在?)

我试图命中的端点是
http://localhost:7071/api/test
当定期运行azure服务器(F5)并通过我的浏览器访问它时,我得到200 ok

你认为会是什么问题?进程的起始参数,还是我只是使用了错误的url?

我现在让它工作了:

private Process process;
private RestClient client;

[OneTimeSetUp]
public void OneTimeSetUp()
{
    //Path to azure cli:
    const string functionHostPath = @"C:\Users\MyName\AppData\Local\AzureFunctionsTools\Releases\2.24.0\cli\func.exe"; 
    //Path to project build location:
    const string functionAppFolder = @"C:\Users\MyName\Source\Repos\ShyftApi\ShyftApi\bin\Debug\netstandard2.0"; 
    int port = 7001;

    process = new Process
    {
        StartInfo =
        {
            FileName = functionHostPath,
            Arguments = $"start -p {port}",
            WorkingDirectory =  functionAppFolder
        }
    };

    bool success = process.Start();
    Assert.AreEqual(true, success);
    client = new RestClient($"http://localhost:{port}/api");
    Thread.Sleep(5000); //Wait for server to initialize
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
    process.CloseMainWindow();
    process.Dispose();
}

[Test]
public void SimpleTest()
{
    RestRequest request = new RestRequest("test", Method.GET);
    IRestResponse response = client.Execute(request);
    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

如果你解释了为了让你的解决方案生效你必须改变什么,我会投票支持你的答案
private Process process;
private RestClient client;

[OneTimeSetUp]
public void OneTimeSetUp()
{
    //Path to azure cli:
    const string functionHostPath = @"C:\Users\MyName\AppData\Local\AzureFunctionsTools\Releases\2.24.0\cli\func.exe"; 
    //Path to project build location:
    const string functionAppFolder = @"C:\Users\MyName\Source\Repos\ShyftApi\ShyftApi\bin\Debug\netstandard2.0"; 
    int port = 7001;

    process = new Process
    {
        StartInfo =
        {
            FileName = functionHostPath,
            Arguments = $"start -p {port}",
            WorkingDirectory =  functionAppFolder
        }
    };

    bool success = process.Start();
    Assert.AreEqual(true, success);
    client = new RestClient($"http://localhost:{port}/api");
    Thread.Sleep(5000); //Wait for server to initialize
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
    process.CloseMainWindow();
    process.Dispose();
}

[Test]
public void SimpleTest()
{
    RestRequest request = new RestRequest("test", Method.GET);
    IRestResponse response = client.Execute(request);
    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}