python - Pytest 在测试文件夹中找不到文件
问题描述
我正在尝试使用 moto 在 pytest 创建一个测试,以使用 boto3 将 Cloud Formation 上传到 AWS。
我正在做的测试是这个:
def test_main_success(self, cf):
'Test upload with a new stack'
sys.stdout = mystdout = io.StringIO()
expected_return = (
"Criando teste\n"
"...aguardando resposta da aws...\n"
"Sucesso\n"
)
main(["teste", "teste.yaml"])
content = mystdout.getvalue()
assert content == expected_return
问题是它找不到文件。
FileNotFoundError: [Errno 2] No such file or directory: './resources/teste.yaml'
但是文件在那里,当我尝试脚本时它运行得很好,并获取了文件。
它试图获取的文件位于脚本文件夹中,而不是测试文件夹中
├── cl_uploader
│ ├── cl_uploader.py
│ ├── __init__.py
│ └── resources
│ ├── 1-ec2-with-sg-eip.yaml
│ └── teste.yaml
└── tests
├── resources
│ └── teste.yaml
└── test_cl_uploader.py
pytest 不会从测试所在的文件夹中获取文件?
解决方案
你需要添加一个
__init__.py
在您的测试文件夹中
您的存储库应如下所示:
├── cl_uploader
│ ├── cl_uploader.py
│ ├── __init__.py
│ └── resources
│ ├── 1-ec2-with-sg-eip.yaml
│ └── teste.yaml
└── tests
├── resources
│ └── teste.yaml
├── __init__.py # init here ! :)
└── test_cl_uploader.py```
验证您的 pytest 配置文件(又名 pytest.ini)是否为相同模式定义了发现策略y
推荐阅读
- java - Maven 在所有错误的地方寻找依赖项(对于某些事情)
- python - 如何在maptplotlib中显示图形两侧的轴标签?
- create-react-app - .eslintrc.js 文件被 create react app 和 craco 忽略
- css - Flexbox 对齐内容空间
- google-cloud-platform - Google Cloud Function Deploying Function OCR-Extract 问题
- c# - IEnumerable.contains 在集合中找不到匹配项,该集合确实存在
- coldfusion - 在 ColdFusion 中使用 java 对象/类的步骤是什么...?
- iis - ADO 发布 IIS Web App 管理任务物理路径作为表达式
- python - 使用连接器时Mysql连接器语法错误python
- ios - SwiftUI 加载 JSON 文件并在视图加载时对其进行过滤