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

问题描述 投票:0回答:1

我想知道有没有人想好如何对实现Azure Functions的类进行功能测试。

首先,有这样一个 好文章 关于如何为Visual Studio (2019) Azure Functions项目模板生成的函数类设置测试。 问题是我的Azure Functions实现使用了依赖注入,不能使用像新的Azure Functions项目中生成的静态类。 我的实现使用了一个 Startup 类,类似于你在 本文 关于Azure函数与依赖注入。

我已经包含了一个简单的函数方法,它可以返回系统版本。 以下是该函数的内容 Startup 和函数类的样子。

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

作为参考,这是一个实现控制器类的Web API项目的功能测试类的样子。 请注意 Startup 下面列出的班级类似于,但又不同于。Startup 的类 SomeFunctions 类。

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 类。 如果我做一些类似于上面Web API项目的实现,我得到了这样的错误信息:"System.InvalidOperationException: 'The provided Type 'Startup' does not belong to assembly with entry point:

System.InvalidOperationException: 'The provided Type 'Startup' does not belong to an assembly with an entry point. 导致此错误的一个常见原因是提供了一个来自类库的Type'。

如何实现Azure Functions中的功能测试类?SomeFunctions 上面的实现?

.net-core dependency-injection azure-functions xunit functional-testing
1个回答
0
投票

我能够找到一个 大文章 关于如何使用 Azure Functions 进行集成测试。 这个测试实现并不完全是一个完整的功能测试,即向URL发送HTTP请求,但它足以测试从Azure Function方法实现的范围向内的完整应用栈。

测试类的要领是这样的。

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.