local.settings.json in Integration/Unit Tests
See original GitHub issueIs 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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’sValues
element into an environment variable):Usage Example:
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
Thank you, @asears, for those links.