Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read appsettings json values in .NET Core Test Project

My Web application needs to read the Document DB keys from appsettings.json file. I have created a class with the key names and reading the Config section in ConfigureServices() as:

public Startup(IHostingEnvironment env) {     var builder = new ConfigurationBuilder()         .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)         .AddEnvironmentVariables();      Configuration = builder.Build(); }  public IConfigurationRoot Configuration { get; }  public void ConfigureServices(IServiceCollection services) {     services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());     services.AddSession();     Helpers.GetConfigurationSettings(services, Configuration);     DIBuilder.AddDependency(services, Configuration); } 

I'm looking for the ways to read the Key values in Test project.

like image 993
S.Siva Avatar asked Sep 30 '16 12:09

S.Siva


People also ask

How do you read values from Appsettings json in C# .NET core console application?

AddJsonFile($"appsettings. json", true, true); var config = builder. Build(); var connectionString = config["ConnectionString"]; var emailHost = config["Smtp:Host"]; Console. WriteLine($"Connection String is: {connectionString}"); Console.

How do I use IConfiguration in .NET core?

Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.


2 Answers

This is based on the blog post Using Configuration files in .NET Core Unit Test Projects (written for .NET Core 1.0).

  1. Create (or copy) the appsettings.test.json in the Integration test project root directory, and in properties specify "Build Action" as Content and "Copy if newer" to Output Directory. Note that it’s better to have file name (e.g. appsettings.test.json ) different from normal appsettings.json, because it is possible that a file from the main project override the file from the test project, if the same name will be used.

  2. Include the JSON Configuration file NuGet package (Microsoft.Extensions.Configuration.Json) if it's not included yet.

  3. In the test project create a method,

     public static IConfiguration InitConfiguration()          {              var config = new ConfigurationBuilder()                 .AddJsonFile("appsettings.test.json")                  .AddEnvironmentVariables()                   .Build();                  return config;          } 

AddEnvironmentVariables (suggested in @RickStrahl blog ) is useful if you want to pass some secrets, that you prefer not store in appsettings.test.json

  1. Use the configuration as usual

     var config = InitConfiguration();  var clientId = config["CLIENT_ID"] 

BTW: You also may be interesting in reading the configuration into the IOptions class as described in Integration test with IOptions<> in .NET Core:

var options = config.Get<MySettings>(); 
like image 111
Michael Freidgeim Avatar answered Oct 05 '22 13:10

Michael Freidgeim


Add the configuration file

First, add a appconfig.json file to the Integration test project

Configure the appconfig.json file to be copied to the output directory by updating

enter image description here

Add NuGet package

  • Microsoft.Extensions.Configuration.Json

Use the configuration in your unit tests

[TestClass] public class IntegrationTests {     public IntegrationTests()     {         var config = new ConfigurationBuilder().AddJsonFile("appconfig.json").Build();                  _numberOfPumps = Convert.ToInt32(config["NumberOfPumps"]);          _numberOfMessages = Convert.ToInt32(config["NumberOfMessages"]);          _databaseUrl = config["DatabaseUrlAddress"];     } }  
like image 33
Amir Touitou Avatar answered Oct 05 '22 11:10

Amir Touitou