如何为azure函数v1编写单元测试

编程入门 行业动态 更新时间:2024-10-22 14:00:13
本文介绍了如何为azure函数v1编写单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

美好的一天,b $ 我写了一个azure函数v1 HttpTrigger,它从我们的api服务获取客户端详细信息和事务,只有一个参数是"频率" ;这也是可选的,所以当功能被触发时,它将获得详细信息然后获得每个零售商详细信息的交易并返回每个零售商的交易费用列表,我想为我的功能编写单元测试但是我我无法看到我的场景的一个好例子,有人可以给我一个示例单元测试(如果可能的话,使用moq),这里是函数代码库示例:

Good Day, I wrote an azure function v1 HttpTrigger that gets client details and transactions from our api service, there is only one parameter which is "frequency" which is optional also, so when the function is triggered it will get the details then get transactions for each retailer details and returns a list of transaction fee for each retailer, I want to write a unit test for my function but I am not able to see a good example for my scenario , Can someone give me an example unit test (with moq if possible) , here is the function codebase example:

[FunctionName("FunctionTest1")]public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function)]HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); #region Properties string Frequency = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "frequency", true) == 0).Value; #endregion log.LogInformation("Getting client details from MongoDB"); Process of Getting ClientDetails log.LogInformation("Get and compute Transaction Fee foreach retailers from client details"); Process of Getting And Computing Transactions for each retailers log.LogInformation("Return results response"); return txnList == null ? new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject("Error getting data from server"), Encoding.UTF8, "application/json") } : new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(txnList, Newtonsoft.Json.Formatting.Indented), Encoding.UTF8, "application/json") }; }

我试过的单元测试参考: docs.microsoft/en-us/azure/azure-functions/functions-test-a功能 medium/@tsuyoshiushio/writing-unit-test-for-azure-durable-functions-80f2af07c65e $

References for unit test I tried:docs.microsoft/en-us/azure/azure-functions/functions-test-a-function medium/@tsuyoshiushio/writing-unit-test-for-azure-durable-functions-80f2af07c65e

推荐答案

我不确定这是否是最好的方法,但我确实想出了一些"只是"的东西。工作。我希望它至少可以帮助你开始

I'm not sure if this is the best way to do this but I did manage to come up with something that "just" worked. I hope it would at least help you to start off using Microsoft.Azure.WebJobs.Host; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Diagnostics; using System.Net.Http; using System.Text; using UnitTestV1; namespace UnitTestV1Test { [TestClass] public class UnitTest1 { protected TraceWriter log = new VerboseDiagnosticsTraceWriter(); [TestMethod] public async void TestFunction1_Query() { var expected = "\"Hello Azure\""; // This seems to because the response is converted to valid JSON var res = await Function1.Run(CreateReq("localhost/func?name=Azure", ""), log); var actual = await res.Content.ReadAsStringAsync(); Assert.AreEqual(expected, actual, "Failed!"); } [TestMethod] public async void TestFunction1_Body() { var expected = "\"Hello Azure\""; // This seems to because the response is converted to valid JSON var res = await Function1.Run(CreateReq("", "{\"name\": \"Azure\"}"), log); var actual = await res.Content.ReadAsStringAsync(); Assert.AreEqual(expected, actual, "Failed!"); } public HttpRequestMessage CreateReq(string uri, string body) { var req = new HttpRequestMessage(); req.SetConfiguration(new System.Web.Http.HttpConfiguration()); if (!String.IsNullOrEmpty(uri)) { req.RequestUri = new Uri(uri); } if (!String.IsNullOrEmpty(body)) { req.Content = new StringContent(body, Encoding.UTF8, "application/json"); } return req; } } public class VerboseDiagnosticsTraceWriter : TraceWriter { public VerboseDiagnosticsTraceWriter() : base(TraceLevel.Verbose) { } public override void Trace(TraceEvent traceEvent) { Debug.WriteLine(traceEvent.Message); } } }

更多推荐

如何为azure函数v1编写单元测试

本文发布于:2023-11-15 13:51:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   函数   单元测试   azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!