I am building a unit testing in my azure devops build pipeline. I am using the python library pytest. In one of my test cases, I am retrieving the environment variables. However, when I run the unit testing, the environment variable is not being retrieve so my testing fails.
This is the yaml of my build
- script: |
pip install pytest pytest-cov
echo %AUTOMATION_ENV%
python -m pytest -v ./test/general/test_identity.py
python -m pytest -v ./test/general/test_initialization.py
displayName: 'Unit test'
enabled: false
env:
AUTOMATION_ENV: unittest
When the echo runs, it successfully prints my variable but for some reason the unit testing can’t get it.
I am running this on a Windows agent.
I tried to do the unit testing in Github action to verify if there is a problem with the logic but it is running properly using this steps
export AUTOMATION_ENV=unittest
pip install pytest pytest-cov
python -m pytest -v ./test/general/test_identity.py
python -m pytest -v ./test/general/test_initialization.py
in github, it runs on ubuntu linux system. Im’m not sure if this plays a role in the error.
2
Answers
On Windows agents, the
script
task (a shortcut of Command line task) will useCMD
to execute the script by default. Similarly, on Linux agents, it will useBash
to execute the script by default.For your case, you can try to check with the following things to fix the issue in your pipeline:
Use the Bash task or PowerShell task to execute the script.
Use the Linux agents to run the pipeline job.
If you still want to use the Windows agent with CMD to execute the script, try to update your script like as below based on the statement here.
I create a sample for your case for both ubuntu agent and windows agent.
test_identity.py
test_initialization.py
test.yaml
Outputs: both echo and pytest works well:
ubuntu job:
windows job:
just copy and paste and modify the file path then try yourself. hope this helps.