Azure Functions has recently added a new feature that allows precompiled assembly to run functions. This gives us a great confidence with regards to unit testing. In this post, we are walking through how to unit test functions with ease, like which we do tests everyday.

The sample code used in this post can be found at HERE.

Function without Dependency

We’re not digging down precompiled function too much as it’s on the document. Let’s have a quick look at the HTTP trigger function code:

Nothing special. Now, we’re writing a test code for this function using xUnit and FluentAssertions.

How does it look like? It’s the same unit test code as what we do everyday. Let’s move on.

Function with Dependency

As I wrote Managing Dependencies in Azure Functions on the other day, dependency management is a bit tricky for Azure Functions due to its static nature. Therefore, we should introduce Service Locator Pattern for dependency management (or injection). Here’s the sample function code:

As we can see the code above, value is retrieved from the service locator instance. Of course, this is just a simple implementation of service locator pattern (If we need more sophisticated one, we should consider an IoC container library like Autofac). And here’s the poorman’s service locator:

Let’s see the test code for the function with dependencies. With the service locator, we can inject mocked object for unit testing, which is convenient for developers. In order for mocking, we use Moq in the following test code.

We create a mocked instance and inject it into the service locator. Then the injected value (or instance) is consumed within the function. How different is it from the everyday testing? There’s no difference at all. In other words, implementing a service locator gives us the same development experiences on Azure Functions, from the testing point of view.

I wrote another article for testing a few months ago, using ScriptCs. This used to be one approach, when Azure Functions didn’t support the precompiled assemblies. Now, we have precompiled functions supported. Therefore, I hope this post would be useful to design functions with better testability.

Application Development and Integration
, ,

Join the conversation! 5 Comments

  1. GREAT article. Can you possibly add an example of using the TraceMonitor to log to both file and console while running tests with XUnit? If the console thing isn’t possible because XUnit doesn’t allow writing to console, perhaps you could show how to use the ITestOutputHelper of XUnit in tandem with TraceMonitor.

    • @jerrywiltse Thanks for your comment. I like your idea. I’ll have a look and write another post (or update this one) regarding to the logging.

      • Despite having the Azure Functions CLI, I would still like to be able to run an integration test in Visual studio. Someone in slack said that instead of TraceWriter, the Run Signatures of functions can take ILogger but I did not verify. Did you have any progress with logging functions locally in VS?

      • @jerry Hi, Jerry.

        I’m sure you can do use ILogger, instead of TraceWriter. At the recent Build 2017, Azure Functions Team and VS Team announced that Functions can have use ILogger now.

        MSDN is currently showing off YSOD, but you can have a look at the post above, when it’s back.

        My post will be coming soon. Stay tuned! lol

  2. […] If you are concerned at dependencies, consider the service locator pattern. Testing Precompiled Azure Functions explained well how to apply service locator pattern in Azure […]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: