I am attempting to run tests from a python module using Azure DevOps. I've got a pipeline build set up to build off a yml file. I'm getting an error that my module name on my imports is not right, but it doesn't look like that the module name is getting set in the build?
When I run this locally, it works just fine.
Module repo file structure:
api_check
api_check/auth
api_check/etc
api_check/__init__.py
I'm running the tests using the following in my yml file: - script: | mkdir logs pytest -m smoke --junitxml=logs/test-results.xml --ENV=prod displayName: 'Test with pytest'
This step is giving the following error:
2018-10-11T21:51:02.9825682Z _______________________ ERROR collecting test_search.py ________________________
2018-10-11T21:51:02.9826322Z ImportError while importing test module '/home/vsts/work/1/s/test_search.py'.
2018-10-11T21:51:02.9826804Z Hint: make sure your test modules/packages have valid Python names.
2018-10-11T21:51:02.9827123Z Traceback:
2018-10-11T21:51:02.9827411Z test_search.py:12: in <module>
2018-10-11T21:51:02.9827745Z from api_check.auth import get_Token_UsernamePassword
2018-10-11T21:51:02.9828644Z E ModuleNotFoundError: No module named 'api_check'
Is there something I need to add to get the build to recognize the module api_check? Is there some sort of build variable I need to read in?
Edit for more information-
I am running this from whatever the directory is that the yml file gets run from my default - based off the debug log, the working directory is /home/vsts/work/1/s
.
When I list the contents of the working directory, I see all the contents of my module, everything that in my repo is under api_check
.
So, rather than what I would expect from pulling down a repo:
$workingDirectory/api_check
$workingDirectory/api_check/auth
$workingDirectory/api_check/etc
$workingDirectory/api_check/__init__.py
it is instead:
$workingDirectory
$workingDirectory/auth
$workingDirectory/etc
$workingDirectory/__init__.py
So it looks like just adding the parent directory api_check
wouldn't help, as there appears to be no api_check
directory. Is there some setting I can flip in the pipeline sources to have it include the top-level folder api_check
?
api_check
is not insys.path
. However, it's not clear from where you are runningpytest
to elaborate a proper solution; adding path ofapi_check
parent dir toPYTHONPATH
should be the quick-and-dirty workaround. – hoeflingapi_check
is the git repo root? Check whether you can modify thegit clone
command in your Azure container (e.g.git clone https://my.repo.git -o api_check
) to match the local directory structure. However, this is only a workaround; modifying your git repo structure, pulling theapi_check
package one level down would be more appropriate. Simply create anapi_check
directory in the repo root and move all the python files from root in there. – hoefling