在pytest下导入软件包时出错

问题描述 投票:0回答:1

在我的一段python代码中,我正在使用动态导入:

def load_class(path, class_str):
    """
    :param path: Function to load class dynamically
    :param class_str:
    :return:
    """
    module = importlib.import_module(path)
    return getattr(module, class_str)

我将此功能称为

class_name = "CL_ABC"
path = "test.test_framework.test_a_feature"
testclass = load_class(path, class_name)

我的错误是

ImportError: No module named test_framework.test_a_feature

我正在从具有unittest框架的文件中调用上述代码。假设文件名是test1,如果我将其命名为python test1可以正常工作,但是如果我将其命名为pytest test1,则会遇到错误。

python pytest
1个回答
0
投票

出于某种原因,pytest在sys.path中不包括当前目录。您可以看到,如果您在任何测试中打印该值。

© www.soinside.com 2019 - 2024. All rights reserved.