在 AWS Codepipeline 中使用 pytest 时导入文件不匹配
Posted
技术标签:
【中文标题】在 AWS Codepipeline 中使用 pytest 时导入文件不匹配【英文标题】:Import file mismatch when using pytest in AWS Codepipeline 【发布时间】:2022-01-12 18:25:30 【问题描述】:运行命令coverage run -m pytest -rap --junitxml coverage.xml
后,所有单元和集成测试都在我的本地机器上成功。但是当我尝试在 AWS Codepipeline 中运行测试时,它会返回以下错误:
==================================== 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
我已经在测试包中添加了 init 文件。还尝试在本地删除 pycache 目录,但没有成功。有人出主意吗?
我的结构是这样的:
【问题讨论】:
【参考方案1】:通过添加pytest.ini
文件解决了这个问题。详情请见this answer。
【讨论】:
以上是关于在 AWS Codepipeline 中使用 pytest 时导入文件不匹配的主要内容,如果未能解决你的问题,请参考以下文章
使用 AWS CodePipeline 在 Jenkins 中构建多个 Gradle 项目
Angular 7 应用程序中的 AWS Codepipeline 部署错误
AWS CodePipeline - 使用现有 Dockerrun.aws.json 部署到 Elastic Beanstalk