>> Learn how to test Azure Functions in
this episode of Azure Tips and Tricks.
When you develop Azure Functions locally on your computer,
you should test them with unit tests,
just like you would test other code that you develop.
This sounds more difficult than it is.
Here we are in Visual Studio,
and here you see an Azure Functions that I've created.
This is a default function with an HTTP trigger.
This comes straight out of the box,
when you do File, New,
Azure Function in Visual Studio.
This function is very simple.
It is triggered by a HTTP requests and if
that request has a name in
either the query string or in the request body,
it returns a message saying, Hello name.
The parameters that this function takes are
an HTTP request and a log in the form of a trace writer.
So, to unit test this function,
we just need to simulate
these parameters and I've already done that.
Let's take a look in the test project.
This is a default Visual Studio MS
Tests Projects that you get out of the box.
In here, I've created the HTTP trigger unit test class.
This contains some tests for the function.
This test tests the function by sending it
an HTTP request object with a query parameter with a name in it.
Here, I use this method to create the HTTP requests.
This code uses the mock framework to mock
the behavior of the HTTP request object.
When you mock an object,
the mock object mimics the behavior of it.
That in the code, in the test interact with it as if it were real.
So, in this HTTP request object,
I incorporate the query string and
the body just like in a real request.
Let's go back. So, that's one parameter.
I simulates the second one like this.
This verbose diagnostics writer is
just an implementation of
the trace writer that Azure Function uses.
So, I don't even have to mock this. All right.
So, I have some more unit tests,
like this one that puts the name in the body
and this one that doesn't send the name at all.
Let's test this out using the Test Explorer.
Here I can simply say,
"Run Selected Tests" and they were run,
and they all passed.
That was testing with unit tests.
These just test if the code works,
not if the actual function works.
When it is running, to test that,
I have to send the function
a real HTTP requests and test how it reacts.
We can do that with an integration test.
In this file, I've created an integration test.
This is also irregular test method just
like unit tests. Here's what it does.
First, I start the Azure Function host.
This runs the function on
my local machine using the func.exe runtime.
This is an Azure Function runtime that gets installed on
your machine with the Azure Function tools for Visual Studio.
I tell func.exe to start from the directory of my Azure Function.
Once I've done that, I give the process
five seconds to make sure that everything is up and running.
Next, I send an actual HTTP requests
to do locally running function.
I do that by building the HTTP request as
adjacent body and sending that through
an HTTP client objects to this address.
This is where the function is running.
You can find your address by locally running your Azure Function.
The address will show up in the command prompt
that outputs to function information.
Finally, I check if what the function
returns matches the string in this string.
Once that is done,
I stopped a local function with this method.
Let's see if this works in the test window.
Again, I just say, "Run Selected Tests",
and now it starts to function locally and waits for a bit,
and there it is, this works.
These are just some examples
of how you can test your Azure Functions.
You can incorporate tests like these in
your CI/CD Pipeline to run them every time you build your code.
This helps you to make sure that your functions work.