question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

local.settings.json in Integration/Unit Tests

See original GitHub issue

Is your question related to a specific version? If so, please specify:

Core Tools Version: 96 Commit hash: c54cdc36323e9543ba11fb61dd107616e9022bba Function Runtime Version: 3.0.14916.0

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#

Question

I’m trying to use xUnit to test an Azure Function. After setting up my test environment using the following logic, the test runs; however, the Values in local.settings.json are not loaded into environment variables, as they would be if I ran the function using func start.

What is the recommended way to load those values to environment variables when writing tests like the following?

var startup = new Startup();
var host = new HostBuilder()
	.ConfigureWebJobs(startup.Configure)
	.Build();

var sut = new Function1(....);

Assert.Equal(....);

Thank you, Ben

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
bgribaudocommented, Dec 28, 2020

Hi @v-anvari,

Thanks! The challenge is that when running automated (unit/integration) tests, the function app settings are not automatically getting loaded into environment variables, so I can’t read them out using System.Environment.GetEnvironmentVariable (e.g. the example you linked to).

I ended up creating a helper method that the appropriate test class(es) call which emulates what func start does (reading each key-value pair local.settings.json’s Values element into an environment variable):

static void ConfigureEnvironmentVariablesFromLocalSettings()
{
	var path = Path.GetDirectoryName(typeof(MyClass).Assembly.Location);
	var json = File.ReadAllText(Path.Join(path, "local.settings.json"));
	var parsed = Newtonsoft.Json.Linq.JObject.Parse(json).Value<Newtonsoft.Json.Linq.JObject>("Values");

	foreach (var item in parsed)
	{
		Environment.SetEnvironmentVariable(item.Key, item.Value.ToString());
	}
}

Usage Example:

ConfigureEnvironmentVariablesFromLocalSettings();

var startup = new Startup();
var host = new HostBuilder()
  .ConfigureWebJobs(startup.Configure)
  .Build();

Is something like this the recommended way to make function app settings available during integration testing or is there a better alternative?

Note: The above has some catches, such as potential variable value conflicts due to parallel test execution. Also, depending on the use case, it may be desirable to ensure that environment variables are reset to their original values at test class teardown (code to do this not shown above).

Thanks, Ben

0reactions
bgribaudocommented, Feb 4, 2021

Thank you, @asears, for those links.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Read values from local.setting.json while debugging test
my suspicion is that it is due to the 'debug' being initiated from another project (Test project), and the local.settings.json does not get ......
Read more >
Accessing local settings while unit testing Azure Functions
Either way, Azure Functions load the local.settings.json on startup, creating several environment variables that then we can use in our code. In ...
Read more >
Azure Functions - Part 2 - Unit and Integration Testing
For local development in an Azure Functions project, you can use a local.settings.json to store configuration, which will never leave your local ...
Read more >
Azure Functions local.settings.json Secrets and Source ...
settings.json is that we are now able to safely add all of the settings that our functions are dependent on to source control....
Read more >
Integration Test for Azure Functions and Cosmos DB ...
Follow my guide below to run your integration test for Azure Function and ... For local development we can use local.settings.json to store ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found