Tel: (01) 7671786

Mon → Sat : 6am-10pm

Email: [email protected]

Facebook

Twitter

LinkedIn

Youtube

Instagram


Environment Files Help You Store Variables

[ad_1]

Note: For this article, I’m going to focus on environment variables for UNIX based operating systems like macOS and Linux.

Environment variables are both a blessing and a curse. They let you easily pass data into processes like applications, scripts, and containers. I develop lots of test automation projects, and environment variables are one of the most common mechanisms for passing test inputs. For example, when I run a test suite against a web app, I might need to set inputs like this:

export BASE_URL="
export USERNAME="pandy"
export PASSWORD="DandyAndySugarCandy"
export SECRET_API_KEY="1234567890abcdefghijklmnopqrstuvwxyz"

I can just run these commands directly in my terminal to set the variables I need. Unfortunately, any time I need to run my tests in another terminal session, I need to repeat the commands to set them again. That’s a big hassle, especially for secrets and long tokens. It would be nice to store these variables in a reusable way with my project.

Thankfully, there is: the environment file. You can create a file named .env and put all your “export” commands for setting variables in it. Basically, just copy those lines above into the .env file. Then, run the following command whenever you want to set those variables in your terminal:

You can verify the value of the variables using the “echo” command. Just remember to prefix variable names with “$“. For example:

The output should be:

I like to create a .env file in every project that needs environment variables. That way, I can easily keep track of all the variables the project needs in one place. I put the .env in the project’s root directory to make it easy to find. Any time I need to run the project, I run the “source” command without any worries.

If the project is stored in a Git repository, then I also add “.env” to the repository’s .gitignore file. That way, my variables won’t be committed to the repository. It’s rude to commit personal settings to a repository, and it’s dangerous and insecure to commit secrets. Many .gitignore templates already include a “.env” entry, too, since using environment files like this is a common practice.


[ad_2]
Source link