Testing Azure Functions locally with automated integration test

I am investing more and more time and efforts into building Azure Functions and using serverless instead of WebApps. Greater stability and balancing makes it my first choice when need high quality API.

Being big fun of automated testing, faced some issues when coding tests and could not find any useful/working examples.

Sources can be found here

Some steps here

Create function with HTTP trigger using template, dont need anything fancy there. Base it on .Net Core 3.1, you can change it, but then check the path in Test project

Create MS Test project

Folder structure

Project structure

Important step, install Azure Function tools from command line, just run

npm i -g azure-functions-core-tools

after that you can test if it is available from command line

The idea is to start functions engine from test project. In local environment you can start it from your command line using Func utility, same can be done from Test project.

From command line it will look like this

From unit tests the same should be done by code and will look like this

        public static void Init(TestContext testContext)
string buildFolder = "Release";
            buildFolder = "Debug";
            var dir = Directory.GetCurrentDirectory();

            var dotnetExePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
            // var dotnetExecutablePath = @"%ProgramFiles%\dotnet\dotnet.exe";
            var functionHostPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
            var functionApplicationPath = dir + @$"\..\..\..\..\..\sources\CoolFunctions\bin\{buildFolder}\netcoreapp3.1";
            var functionAppFolder = Path.GetRelativePath(Directory.GetCurrentDirectory(), functionApplicationPath);

            if (!Directory.Exists(functionApplicationPath))
                throw new FileNotFoundException(functionApplicationPath);

            if (!Directory.Exists(functionAppFolder))
                throw new FileNotFoundException(functionAppFolder);

            _funcHostProcess = new Process
                StartInfo =
                    FileName = dotnetExePath,
                    Arguments = $"\"{functionHostPath}\" start -p {Port}",
                    WorkingDirectory = functionAppFolder

            var success = _funcHostProcess.Start();
            if (!success)
                throw new InvalidOperationException("Could not start Azure Functions host.");

After starting tests, functions will run, then you just need to call endpoint from test methods

        public async Task TestMethod1()
            string parsedData = "";
            var result = await Client.GetAsync("api/Function1?name=armen");

            if (result.IsSuccessStatusCode)
                parsedData = await result.Content.ReadAsStringAsync();

            Assert.AreNotEqual("", parsedData);
            Assert.AreEqual("Hello, armen. This HTTP triggered function executed successfully.", parsedData);


Popular posts from this blog

How to poll database using WCF-SQL adapter

XML debatch in pipeline

Debatching records from WCF-SQL using LOOP and XPath