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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Obfuscation vs Encryption: How to Protect Your .NET Code the Right Way
  • Using a Body With an HTTP Get Method Is Still a Bad Idea
  • Reliability Models and Metrics for Test Engineering

Trending

  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  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
46.9K 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.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Obfuscation vs Encryption: How to Protect Your .NET Code the Right Way
  • Using a Body With an HTTP Get Method Is Still a Bad Idea
  • Reliability Models and Metrics for Test Engineering

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!