使用pytest.mark.parametrize时遇NotADirectoryError报错求助
问题解决:Windows下pytest-embedded日志目录含非法字符导致NotADirectoryError
问题原因
pytest-embedded插件生成日志目录时,默认会将测试参数的repr()结果(如{'x': 2})拼入目录名,但Windows系统禁止目录名包含{、}、:等字符,因此触发错误。参数名设为config时插件有特殊处理,不会直接使用参数的repr结果,所以能正常运行;你的朋友大概率使用Linux/macOS系统,这类系统允许上述特殊字符作为目录名,因此无报错。
解决方案
方案1:在参数化时指定自定义标识(推荐)
在@pytest.mark.parametrize中添加ids参数,为每个测试用例生成合法的目录标识,替换默认的repr格式:
import hashlib import logging from typing import Callable import pytest from pytest_embedded_idf.dut import IdfDut EXPECT_TIMEOUT = 10 testdata = [2, 3, 4] @pytest.mark.esp32s3 @pytest.mark.parametrize("x", testdata, ids=[f"x={val}" for val in testdata]) def test_hello_world(dut: IdfDut, x): pass
这样生成的日志目录名会变成esp32s3.default.test_hello_world.x=2,完全符合Windows的目录命名规则。
方案2:修改pytest-embedded的日志目录命名规则
在项目根目录的conftest.py中添加配置,自定义日志目录的格式,避免使用参数的repr结果:
def pytest_configure(config): # 自定义日志目录格式,替换默认的参数repr展示 config.option.embedded_log_dir_format = '{target}.{app}.{test_name}.{param_name}={param_value}'
该配置会将参数部分格式化为参数名=参数值的形式,避免特殊字符。
方案3:临时规避(不推荐)
如果只是临时测试,可将参数名改为插件特殊处理的名称(如config),但这种方式会限制参数命名的灵活性,仅适合临时场景。
内容的提问来源于stack exchange,提问作者Vedant
相关产品推荐
相关产品推荐

