Jonathan M Fishbein on Mon, 28 Aug 2017 18:33:10

Specifically I would like to pass cosmosdb properties into my precompiled Run azure function from my function.json file for unit testing purposes.  My azure function is an http triggered function and does (for clarity, not yelling) NOT have a cosmosdb binding.  I had a comment convo conversation here 

where I was able to hone into exactly what I was looking for.  Any help on this is greatly appreciated.  Thanks!



AhmedElSayed on Mon, 28 Aug 2017 21:08:29

You can use app settings for that.

Locally, you can put those in local.settings.json in the Values collection, and you'll have access to those values in your functions either through environment variables or ConfigurationManager.AppSettings.


// local.settings.json
  "IsEncrypted": false,
  "Values": {
    "myCosmosDbKey": "this is the value of the key"

in your C# function you can now do

public static HttpResponseMessage Run([HttpTrigger]HttpRequestMessage req, TraceWriter log)
    // you can also use this
    return req.CreateResponse(HttpStatusCode.OK, "Hello");

Note that local.settings.json is only for your local development, on Azure you should use the Azure App Settings feature which you can find in your platform features tab for your function app. Those app settings will be available to your function in exactly the same way as those from local.settings.json. but they are stored encrypted.

if you want to encrypt settings locally as well you can do "func settings encrypt" using the azure-functions-core-tools command line interface.

Software Engineer - Azure Functions

Jonathan M Fishbein on Mon, 28 Aug 2017 22:24:27

Thanks for this detailed response.  

Sorry I left out some details from the other post.  

I am currently using the local.settings.json in conjunction with ConfigurationManager.AppSettings.[""].  When I am attempting to unit test my pre-compiled function, I cannot access the appsettings because the function is Not executing within the azure functions runtime.


Been thinking more on how to explain.  I'd like a way to pass properties from azure appsettings -> function.json -> Run function when in production.  But when I'm unit testing I'd like to skip the appsettings part but have function parameters that will accept the properties directly because the function will Not be executing within the azure functions runtime

AhmedElSayed on Mon, 28 Aug 2017 22:28:53

I see. I don't think there is a way to inject these settings into the unit test context. I think you'll need to pull them down in some test fixture before all tests run.

Jonathan M Fishbein on Mon, 28 Aug 2017 22:47:20

I'm having some difficulty articulating what my need is, I hope it makes sense.  It's a pre-compiled Function so when I want to unit test it, it's just an isolated function in a .dll.  Adding an App.config to the library project wouldn't make sense because I'd be undermining appsettings that should be secrets, and also I'd like to use different values when testing and when in production and I think adding an App.config would override the Azure app settings.  I've submitted a github issue here


So I can unit test my function within the azure functions runtime.  I do host start and then I unit test it by doing a 


and thinking more about it I could alter the local.settings.json file to point to local databases.  But I was hoping I could have a lower layer unit test that excludes the azure functions runtime as a dependency