Search code examples
unit-testingpytestintegration-testingaws-codepipelinemismatch

Import file mismatch when using pytest in AWS Codepipeline


All the unit and integration tests succeed on my local machine after running the command coverage run -m pytest -rap --junitxml coverage.xml. But when I'm trying to run the tests in the AWS Codepipeline it returns the errors below:

==================================== ERRORS ====================================
______ ERROR collecting tests/integration_tests/base_integration_test.py _______
import file mismatch:
imported module 'tests.integration_tests.base_integration_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/integration_tests/base_integration_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/integration_tests/base_integration_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
_____ ERROR collecting tests/integration_tests/gitlab_integration_test.py ______
import file mismatch:
imported module 'tests.integration_tests.gitlab_integration_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/integration_tests/gitlab_integration_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/integration_tests/gitlab_integration_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
___ ERROR collecting tests/integration_tests/kubernetes_integration_test.py ____
import file mismatch:
imported module 'tests.integration_tests.kubernetes_integration_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/integration_tests/kubernetes_integration_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/integration_tests/kubernetes_integration_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
________________ ERROR collecting tests/unit_tests/base_test.py ________________
import file mismatch:
imported module 'tests.unit_tests.base_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/unit_tests/base_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/unit_tests/base_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
________________ ERROR collecting tests/unit_tests/bot_test.py _________________
import file mismatch:
imported module 'tests.unit_tests.bot_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/unit_tests/bot_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/unit_tests/bot_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
_______________ ERROR collecting tests/unit_tests/gitlab_test.py _______________
import file mismatch:
imported module 'tests.unit_tests.gitlab_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/unit_tests/gitlab_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/unit_tests/gitlab_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
_________ ERROR collecting tests/unit_tests/kubernetes_service_test.py _________
import file mismatch:
imported module 'tests.unit_tests.kubernetes_service_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/unit_tests/kubernetes_service_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/unit_tests/kubernetes_service_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
_____________ ERROR collecting tests/unit_tests/kubernetes_test.py _____________
import file mismatch:
imported module 'tests.unit_tests.kubernetes_test' has this __file__ attribute:
  /codebuild/output/src581058806/src/pyhelper/tests/unit_tests/kubernetes_test.py
which is not the same as the test file we want to collect:
  /codebuild/output/src581058806/src/tests/unit_tests/kubernetes_test.py

I already added init files in the test packacges. Also tried to delete pycache directory locally, but no success. Anybody an idea?

My structure looks like this:

folders


Solution

  • Fixed the issue by adding a pytest.ini file. See this answer for more details.