Friday, March 18, 2022

[SOLVED] Pytest not found in Azure DevOps Pipeline

Issue

In my Azure DevOps Pipeline

(https://dev.azure.com/TheNewThinkTank/dark-matter-attractor/_build?definitionId=2&_a=summary)

I have a test stage from where I wish to install and run pytest on all Python files in the corresponding Azure DevOps repository

(https://dev.azure.com/TheNewThinkTank/_git/dark-matter-attractor).

The way Pytest is installed and run follows the official Microsoft documentation:

- script: |
    pip install pytest
    pip install pytest-cov
    pytest tests --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
  displayName: 'Test with pytest'

Which can be found at: https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python?view=azure-devops

Running the Pipeline results in an error at the above step: pytest: command not found

If you wish to see the full traceback:

https://dev.azure.com/TheNewThinkTank/dark-matter-attractor/_build/results?buildId=62&view=logs&j=792982d5-3bb1-5d82-222e-228148b73448&t=1f7e9bd0-3cdf-5b91-30b0-87c9b2af71b7

Other things I have tried:

Following the official pytest site (https://docs.pytest.org/en/stable/getting-started.html), I tried adding a flag to the install command: pip install -U pytest, but with the same outcome.

What is preventing the Pipeline from discovering the installed pytest module?


Solution

Please try python -m pytest:

- script: |
    pip install pytest
    pip install pytest-cov
    python -m pytest tests --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
  displayName: 'Test with pytest'

Modules installed over pip are not recognized as system level command.



Answered By - Krzysztof Madej
Answer Checked By - Candace Johnson (WPSolving Volunteer)