skip to Main Content

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


  1. On Windows agents, the script task (a shortcut of Command line task) will use CMD to execute the script by default. Similarly, on Linux agents, it will use Bash 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:

    1. Use the Bash task or PowerShell task to execute the script.

    2. Use the Linux agents to run the pipeline job.

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

      - script: |
          call pip install pytest pytest-cov
          echo %AUTOMATION_ENV%
          call python -m pytest -v ./test/general/test_identity.py
          call python -m pytest -v ./test/general/test_initialization.py
        displayName: 'Unit test'
        enabled: false
        env:
          AUTOMATION_ENV: unittest
      

    Login or Signup to reply.
  2. I create a sample for your case for both ubuntu agent and windows agent.

    modify the python file path with your own.

    test_identity.py

    import pytest
    import os
    
    def simple_fun2(s: str):
        return s + 'fun2'
    
    #-------test--------#
    env2 = os.environ["AUTOMATION_ENV"]
    
    def test_two():
        assert simple_fun2(env2) == 'unittestfun2'
    

    test_initialization.py

    import pytest
    import os
    
    def simple_fun1(s: int):
        return s + "fun1"
    
    #-------test--------#
    env2 = os.environ["AUTOMATION_ENV"]
    
    def test_one():
        assert simple_fun1(env2) == "unittestfun1"
    

    test.yaml

    jobs:
    - job: ubuntuJob
      pool:
        vmImage: ubuntu-latest
      steps:
      - script: |
          pip install pytest pytest-cov
          echo $AUTOMATION_ENV
    
          python -m pytest -v ./pythonPytest/test_identity.py
          python -m pytest -v ./pythonPytest/test_initialization.py
        env:
          AUTOMATION_ENV: unittest
    
    - job: windowsJob
      pool:
        vmImage: windows-latest
      steps:
      - script: |
          pip install pytest pytest-cov
          echo %AUTOMATION_ENV%
    
          python -m pytest -v ./pythonPytest/test_identity.py
          python -m pytest -v ./pythonPytest/test_initialization.py
        env:
          AUTOMATION_ENV: unittest
    

    Outputs: both echo and pytest works well:

    ubuntu job:

    enter image description here

    windows job:

    enter image description here


    just copy and paste and modify the file path then try yourself. hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search