This is the 29th day of my participation in the August Wenwen Challenge.More challenges in August
Why environment variables
In automated testing, environment variables are sometimes needed to achieve certain goals. Common scenarios include:
- Switching test environment
- Switching test Configuration
- Storing sensitive data (from an information security perspective)
Setting environment Variables
In Linux
Using the export command
$ export UserName=admin
$ echo $UserName
admin
$ export Password=123456
$ echo $Password
123456
Copy the code
Or write variables in a file and source loads
In the Windows
Using the set command
> set a=666
> echo %a%
> 666
Copy the code
Use environment variables in HttpRunner
Using the env.
demo_testcase.yml
config:
name: "demo testcase"
variables:
device_sn: "ABC"
username: ${ENV(USERNAME)}
password: ${ENV(PASSWORD)}
base_url: "https://getman.cn/mock"
output: [
"username"."device_sn"
]
.
Copy the code
demo_api.yml
name: demo api
variables:
var1: value1
var2: value2
request:
url: /phyger
method: GET
headers:
User-Agent: $user_agent
Content-Type: "application/json"
json:
key: $var2
username: $username
pwd: password
Copy the code
Test Report:
Use system environment variables
For demonstration purposes, we delete variables in.env and use variables that already exist in the system
demo_api.yml
name: demo api
variables:
var1: value1
var2: value2
request:
url: /phyger
method: GET
headers:
User-Agent: $user_agent
Content-Type: "application/json"
json:
key: $var2
username: ${ENV(PROCESSOR_ARCHITECTURE)}
pwd: ${ENV(OS)}
Copy the code
The test report
As shown in the figure, the system environment variables have been obtained.
Env and system variables exist at the same time
.env
Test Report:
Conclusion: When. Env and system environment variables exist together,. Env takes precedence over system environment variables.
summary
Environment variables usually exist as specific immutable contents within each environment, and from this point on, we can set variables of this property that we need to use in our projects to environment variables and call them directly in our actual tests.
Function variables & procedure variables refer to input parameters that may change in a method and intermediate variables generated during the execution of the method. These types of variables are usually stored in memory or configuration files. After the project runs, these variables are meaningless.
Thank you for reading, don’t forget to follow, like, comment, forward four consecutive yo!