如何对 Azure Functions 进行功能测试?

How to do Functional Testing for Azure Functions?

我想知道是否有人知道如何在实现 Azure Functions 的 class 上执行功能测试。

首先,nice article about how to set up testing for the function class that is generated by the Visual Studio (2019) Azure Functions project template. The problem is that my Azure Functions implementation uses dependency injection and cannot use static classes like the ones generated in a new Azure Functions project. My implementation uses a Startup class that is similar to what you would find in this article 关于带有依赖项注入的 Azure Functions。

我已经包含了一个简单的函数方法,returns系统版本。这是 Startup 和函数 class 的样子:

[assembly: FunctionsStartup(typeof(SomeNamespace.FunctionApp.Startup))]
namespace SomeNamespace.FunctionApp
{
    public sealed class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSomeService(); // IServiceCollection extension method defined elsewhere.
        }
    }

    public class SomeFunctions
    {
        public SomeFunctions(IConfiguration configuration, ISomeService someService)
        {
            _someService = someService;

            Version currVersion = GetType().Assembly.GetName().Version;
            _systemVersion = new Version(currVersion.Major, currVersion.Minor, configuration.GetValue<Int32>("BuildId")).ToString();
        }

        private readonly ISomeService _someService;
        private readonly String _systemVersion;

        [FunctionName("SystemVersion")]
        public ActionResult<String> SystemVersion([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "SomeFunctions/SystemVersion")] HttpRequest req)
        {
            return new JsonResult(_systemVersion) { StatusCode = (Int32)HttpStatusCode.OK };
        }
    }
}

作为参考,这是实现控制器 class 的 Web API 项目的功能测试 class 的样子。请注意,下面列出的 Startup class 与用于 SomeFunctions class 的 Startup class 相似,但有所不同。

namespace SomeNamespace.WebAPI.Controllers
{
    public sealed class TestSomeController : IClassFixture<WebApplicationFactory<Startup>>
    {
        public TestSomeController(WebApplicationFactory<Startup> factory)
        {
            _client = factory.CreateClient();
        }

        private readonly HttpClient _client;

        [Fact]
        public async Task Get()
        {
            var httpResponse = await _client.GetAsync("api/SomeRoute");
            Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
        }
    }
}

我正在尝试找到一种方法来为上面的 SomeFunctions class 做类似的事情。如果我执行与上述 Web API 项目的实施类似的操作,我会收到此错误消息:

System.InvalidOperationException: 'The provided Type 'Startup' does not belong to an assembly with an entry point. A common cause for this error is providing a Type from a class library.'

如何在上述 SomeFunctions 实施中为 Azure Functions 实施功能测试 class?

我找到了关于如何使用 Azure Functions 执行集成测试的 great article。此测试实现不是完整的功能测试,其中将 HTTP 请求发送到 URL,但足以从 Azure Function 方法实现的范围向内测试完整的应用程序堆栈。

测试的要点 class 如下所示:

namespace SomeNamespace.FunctionApp
{
    public sealed class TestSomeFunctions
    {
        public TestSomeFunctions()
        {
            var startup = new Startup();
            var host = new HostBuilder()
                .ConfigureWebJobs(startup.Configure)
                .Build();

            _someFunctions = new SomeFunctions(SomeFunctions);
        }

        private readonly SomeFunctions _someFunctions;

        [Fact]
        public void SystemVersion()
        {
            var request = new DefaultHttpRequest(new DefaultHttpContext());
            JsonResult jsonResult = _someFunctions.SystemVersion(request).Result as JsonResult;
            Assert.Equal((Int32)HttpStatusCode.OK, jsonResult.StatusCode);
        }
    }
}