简体   繁体   中英

How do environment variables know what environment I am running on and how do I set them in C#?

I am reading a selenium guidebook for c# and they show this:

class BaseTest
 {
 private static string VendorDirectory = System.IO.Directory.GetParent(
 System.AppContext.BaseDirectory).
 Parent.Parent.Parent.FullName
 + @"/vendor";
 protected IWebDriver Driver;
 public static string BaseUrl;
[SetUp]
 protected void SetUp()
 {
 BaseUrl = System.Environment.GetEnvironmentVariable("BASE_URL") ??
"http://the-internet.herokuapp.com";

But it doesn't show how they are actually setting the environment variables. Is BASE_URL coming from appsettings.json? I'm not sure where they are getting it from. Right now, I have a class with all of the urls I am using throughout my tests like this:

public static class Urls
{
    public static readonly string baseUrl = "https://localhost:5001/";
    public static readonly string aboutUrl = $"{baseUrl}about";
    public static readonly string citiesUrl = $"{baseUrl}cities";
    public static readonly string countriesUrl = $"{baseUrl}countries";
}

I don't think this is the best way to do it and want to try to use environment variables instead but I am not sure how to do that. When I change from localhost to a production environment how I have it now will obviously break. How can I set the baseUrl so it knows what environment I am in?

EDITED My test solution is in a separate repo from my project solution. My test solution is a c# xunit test project. I added an appsettings.json file to my solution. It looks like this

{
  "Base_Url": "https://testurl/",
  "AllowedHosts": "*"
}

Inside one of my tests that uses the url, I am doing this

        public static IConfiguration InitConfiguration()
        {
            var config = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json")
               .Build();
            return config;
        }
        [Fact]
        public void LoadFaqs()
        {
            using IWebDriver driver = new ChromeDriver();
            var config = InitConfiguration();

            var faqurl = config["Base_Url"] + "faqs";
            driver.Navigate().GoToUrl(faqurl);
         }

When I run my test, it is failing because it cannot find my appsettings.json file. I also tried putting it inside my test folder and it still couldn't find it. I'm not sure what I am doing wrong.

Environment variable are set by the Windows system with predefined values the most common ENV variable is PATH you can display env variables on cmd with: echo %ENV_NAME% for example echo %PATH% gives you:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;

Env variables is a way to pass parameters between a parent process to a child process ( in the cases where args on command lines are a problem) Then env context is only known by the child process and disappear once the process is killed. Your selenium launcher might set additional variable with win api like:

public static void SetEnvironmentVariable (string variable, string? value);

SetEnvironmentVariable

In your example I would say: do not care about how selenium set this env variable, just take the content of BASE_URL ( hoping it is not empty....)

Locally, they must be setting them up by using either the project configuration (this is for Visual Studio):

Project properties -> Debug tab -> Environment variables

项目属性 -> 调试选项卡 -> 环境变量

or by running some MSBuild scripts/tasks on build events that ensure the environment variables are added. ( quick search on SO ). Or by adding them manually on the code when the test host is being built somehow (I'm guessing this is for some functional or integration testing)

In CI/CD pipelines they are set up depending on the platform you're using (Github, Gitlab, Azure DevOps etc)

The downside of using hard-coded classes for configuration is that whenever the configuration changes (like when you change your environment and instead of pointing to a local api you need to point to the production one), you need to change/recompile your code, like you said.

So yeah, I'd say have a look and explore those options. Configuration can use several configuration providers . On asp.net core, by default, they use one called ChainedConfigurationSource , which includes reading from the appsettings.json files, environment variables etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM