To Unit Testing HTTP triggered Azure Functions with test data is well and often described. For example here: How to write unit test for azure function v1
Mock data are given in the http request.
But I have a timer triggerd Azure function which reads data from a FTP server. I would like to Unit Test the function with test data.
My function:
[FunctionName("MyTimerTriggeredFunction")]
public static void Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
ServiceClass serviceClass = new ServiceClass(log, context);
List<Order> orderList = serviceClass.getOrdersFromFtp();
...
a running Unit Test function to test the logger, just to show how I started:
public void TestLogger()
{
// https://learn.microsoft.com/de-de/azure/azure-functions/functions-test-a-function
var logger = (ListLogger)TestFactory.CreateLogger(LoggerTypes.List);
MyTimerTriggeredFunction.Run(null, logger, null);
var msg = logger.Logs[0];
bool testString = msg.Contains("C# Timer trigger function executed at");
Assert.IsTrue(testString);
}
serviceClass.getOrdersFromFtp() return a list of objects. I could create this list with mock data in the unit test but how to give it to the timer triggered azure function?
You should move your business logic outside of the azure function and test that, rather than coming up with ways to mock data for a timer-triggered function.
Your code would look something like this:
[FunctionName("MyTimerTriggeredFunction")]
public static void Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
ServiceClass serviceClass = new ServiceClass(log, context);
serviceClass.DoWork();
...
class ServiceClass
{
public void DoWork()
{
List<Order> orderList = serviceClass.getOrdersFromFtp();
...