【问题标题】:How to unit test Azure Functions in Visual Studio如何在 Visual Studio 中对 Azure Functions 进行单元测试
【发布时间】:2020-01-17 04:33:54
【问题描述】:

我正在使用 Visual Studio 创建 Azure Functions。我可以手动创建、发布和运行函数。如果我将我的 Function 项目设置为启动并运行,我将启动主机。使用 MSTest 时如何让 Host 启动?

我想使用 RestSharp 编写测试并在测试期间调用函数 - 实际应用程序的工作方式。看来我需要一种方法让 MSTest 启动 Azure Function Host。

【问题讨论】:

  • 为什么需要运行该函数进行单元测试?
  • 虽然我发现可以在不运行函数的情况下运行测试,即docs.microsoft.com/en-us/azure/azure-functions/… 我不知道如何使用 RestSharp 调用测试,除非它们实际上在主机中运行。如果没有经过测试的 RestSharp 实现,我真的无法知道正确的调用是什么样的。我们曾经能够在旧的 VS 测试框架中使用 Asp.Net 做这样的事情
  • 为什么还需要使用 RestSharp 来测试它们?
  • 因为我就是这样使用它们的
  • 有没有更好的办法?

标签: visual-studio azure-functions azure-functions-core-tools


【解决方案1】:

我希望找到一种类似于在旧版本的 VS 中调试 Asp.Net/SOAP 的方法,其中 MSTest 引擎将启动 IISExpress 并将 VS 附加到 Asp.Net 项目。支持编辑并继续。

到目前为止,我已经制定了以下方法:

RestSharp 代码:

    var url = $"http://localhost:7071/api";
    var functionKey = "this value is ignored by 'Azure Functions Core Tools' ";

    var client = new RestSharp.RestClient(url);
    var request = new RestSharp.RestRequest("GetConnectionString", RestSharp.Method.POST);
    request.AddHeader("x-functions-key", functionKey);

    var response = client.Execute<string>(request);
  • 选项 1:

运行 VS 的第二个实例并运行函数。

更新代码以反映 Azure Functions 核心工具显示的 url 路径。

编辑并继续工作。

  • 选项 2:

将此类添加到 UnitTest 项目中:

附加到 func.exe 进程以调试函数

编辑并继续不起作用。

   [TestClass]
    public class Initializer
    {
        static System.Diagnostics.Process process { get; set; }

        [AssemblyInitialize]
        public static void Initialize(TestContext context)
        {
            //process does not use the WorkingDirectory properly with %userprofile%
            var userprofile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            var path = $@"{userprofile}\source\repos\mypro\...\myproj.Functions\bin\Debug\netcoreapp3.1";

            var startInfo = new ProcessStartInfo
            {
                FileName = @"cmd.exe",
                Arguments = @"/C ""%localappdata%\AzureFunctionsTools\Releases\3.2.0\cli_x64\func.exe host start --port 7071 --pause-on-error""",
                //RedirectStandardInput = true,
                //RedirectStandardOutput = true,
                //RedirectStandardError = true,
                //UseShellExecute = false,

                UseShellExecute = true,
                WorkingDirectory = path
            };

            process = new System.Diagnostics.Process()
            {
                StartInfo = startInfo

            };
            process.Start();

       //     Thread.Sleep(3000);

        }

    [AssemblyCleanup]
    public static void Cleanup()
    {
        process.CloseMainWindow();
    }
}
  • 选项 3:

启动func.exe进程

工作文件夹:...\source\repos\xxx\xxx\xxx.Functions\bin\Debug\netcoreapp3.1\

命令行:

"C:\Users\username\AppData\Local\AzureFunctionsTools\Releases\3.2.0\cli_x64\func.exe" host start --port 7071 --pause-on-error

附加到 func.exe 进程。

编辑并继续不起作用。

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2010-10-06
    • 2012-10-10
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多