【问题标题】:Azure functions integration testing locally本地 Azure 功能集成测试
【发布时间】: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


    【解决方案1】:

    我现在开始工作了:

    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);
    }
    

    【讨论】:

    • 如果你解释了你必须改变什么才能让你的解决方案正常工作,我会赞成你的回答
    猜你喜欢
    • 2011-04-09
    • 2019-03-26
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2020-09-25
    • 2021-04-06
    • 2016-08-27
    • 1970-01-01
    相关资源
    最近更新 更多