DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Quality Assurance in AI-Driven Business Evolution
  • The New Testing Pattern: Standardizing Regression for Cloud Migrations
  • From Test Automation to Autonomous Quality: Designing AI Agents for Data Validation at Scale
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Trending

  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  1. DZone
  2. Data Engineering
  3. Data
  4. Read Config Data in .NET Core Test Project: .NET Core Quick Posts, Part IV

Read Config Data in .NET Core Test Project: .NET Core Quick Posts, Part IV

In this post, we will look at how to read configuration data in .NET Core test projects with a few quick and easy steps.

By 
Neel Bhatt user avatar
Neel Bhatt
·
Aug. 27, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
47.2K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we will see how to read configuration data in .NET Core test projects.

We'll need some configurable data which we can change once the application is deployed. Things have changed a bit in .NET Core when it comes to reading configuration data.

Let us see how to do it. 

Prerequisite:

  • Visual Studio 2017 community edition, download here.
  • .Net Core 2.0 SDK from here (I have written a post to install SDK here).

Create the Test Project Using .NET Core 2.0 Template in VS 2017

Once you have all these installed, open your Visual Studio 2017 -> Create New Project -> Select xUnit Test Project:

Visual Studio will create a well-structured application for you.

Add a Config JSON File

Let us add the config file from which we will read the configuration key-value pair.

I named the file as my-config.json. Add below values for now:

{
  "my_Key_1": "value1",
  "my_key_2": "value2"
}

Right-click on the my-config.json file -> click on Properties -> select Copy if newer:

Once this is done, build the project.

Add a NuGet Package

Next step is to add the NuGet pckage which we will require to read the data from the JSON file which we just added.

Search with "Microsoft.Extensions.Configuration.Json" in the NuGet Package Manager and click on Install:

This Nuget package is a JSON configuration provider implementation for Microsoft.Extensions.Configuration.

Use it in the Test Method

Now let us use this NuGet package to read the configuration data.

Add the below code in the test method:

var config = new ConfigurationBuilder()
 .AddJsonFile("my-config.json")
 .Build();

var clientId = config["my_Key_1"];

Here, ConfigurationBuilder is part of Microsoft.Extensions.Configuration.

That is it. Now we can read the config data as you can see below:

value1 is returned which is the value of the key, my_key_1.

Hence you can read the config data for the .NET Core test project.

Hope this helps.

.NET Data (computing) Testing POST (HTTP)

Published at DZone with permission of Neel Bhatt. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Quality Assurance in AI-Driven Business Evolution
  • The New Testing Pattern: Standardizing Regression for Cloud Migrations
  • From Test Automation to Autonomous Quality: Designing AI Agents for Data Validation at Scale
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook