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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Neel Bhatt user avatar by
Neel Bhatt
CORE ·
Aug. 27, 18 · Tutorial
Like (4)
Save
Tweet
Share
45.95K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are the Benefits of Java Module With Example
  • Introduction to Spring Cloud Kubernetes
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: