Last active
September 23, 2019 07:42
-
-
Save malteb247/f6afef9a6a99991c4bb9e18ed1e52972 to your computer and use it in GitHub Desktop.
An example of how to use appsettings in NUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
namespace Jaber.Tests | |
{ | |
using Microsoft.Extensions.Configuration; | |
/// <summary> | |
/// Just an example class that contains my special configuration | |
/// </summary> | |
public class MySpecialConfig | |
{ | |
public bool EnableSomething { get; set; } | |
public int Timeout { get; set; } | |
} | |
/// <summary> | |
/// A simple class to demonstrate access to appsettings.json | |
/// </summary> | |
public static class TestUtilities | |
{ | |
/// <summary> | |
/// The simplest approach | |
/// </summary> | |
/// <param name="basePath"></param> | |
/// <returns><see cref="IConfigurationRoot"/></returns> | |
/// <remarks> | |
/// Requires 'Microsoft.Extensions.Configuration.Json' | |
/// </remarks> | |
/// <example> | |
/// Usage with NUnit's <see cref="TestContext"/> | |
/// var configRoot = TestUtilities.GetConfiguration(TestContext.CurrentContext.TestDirectory); | |
/// var myValue = configRoot["MySpecialConfig:Timeout"]; | |
/// </example> | |
public static IConfigurationRoot GetConfiguration(string basePath) | |
{ | |
return new ConfigurationBuilder() | |
.SetBasePath(basePath) | |
.AddJsonFile("appsettings.json", optional: true) | |
// Optional | |
//.AddEnvironmentVariables() | |
.Build(); | |
} | |
/// <summary> | |
/// The type safe way | |
/// </summary> | |
/// <param name="basePath"></param> | |
/// <param name="sectionName"></param> | |
/// <returns>New instance of <see cref="MySpecialConfig"/></returns> | |
/// <example> | |
/// Usage with NUnit's <see cref="TestContext"/> | |
/// var myConfig = TestUtilities.GetMySpecialConfig(TestContext.CurrentContext.TestDirectory); | |
/// var myValue = myConfig.Timeout; | |
/// </example> | |
public static MySpecialConfig GetMySpecialConfig(string basePath, string sectionName = nameof(MySpecialConfig)) | |
{ | |
var newInstance = new MySpecialConfig(); | |
var config = GetConfiguration(basePath); | |
config | |
.GetSection(sectionName) | |
.Bind(newInstance); | |
return newInstance; | |
} | |
/// <summary> | |
/// Does it flexible and type safe | |
/// Uses the convention "Section name == class name" | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="basePath"></param> | |
/// <param name="sectionName"></param> | |
/// <returns>New instance of <see cref="T"/></returns> | |
/// <example> | |
/// Requires 'Microsoft.Extensions.Configuration.Binder' | |
/// Usage with NUnit's <see cref="TestContext"/> | |
/// var myConfig = TestUtilities.GetConfiguration<MyConfig>(TestContext.CurrentContext.TestDirectory); | |
/// var myValue = myConfig.Timeout; | |
/// </example> | |
public static T GetConfiguration<T>(string basePath, string sectionName = null) where T : new() | |
{ | |
var newInstance = new T(); | |
var config = GetConfiguration(basePath); | |
config | |
.GetSection(sectionName ?? typeof(T).Name) | |
.Bind(newInstance); | |
return newInstance; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment