【问题标题】:python nose don't change working directory pathpython鼻子不改变工作目录路径
【发布时间】:2015-12-17 17:17:42
【问题描述】:

我的鼻子有问题。 我想在测试目录中运行测试,但我不希望鼻子更改工作目录。这是因为我在我的库和单元测试本身中使用了相对导入。

首先是目录结构:

app_dir
    app.py
    library_dir
        tests_dir
            test_1.py
            test_2.py

我目前正在 app_dir 目录中运行这样的测试:

python -m unittest discover -s library_dir.tests_dir

这很好用。

现在,我想用鼻子(nosetests)来做这个。

当我尝试时,我得到以下信息:

nosetests library_dir.tests_dir --collect-only -vv
Failure: SystemError (Parent module '' not loaded, cannot perform relative import) ... ok
Failure: SystemError (Parent module '' not loaded, cannot perform relative import) ... ok

我已经看过关于 SO 的类似问题(有很多),我可以看到的一个关于这个问题的建议是从 unittest 本身中删除相对导入,但我喜欢这种模式。此外,如果我确实更改为绝对导入,我会得到一个

(No module named '<the_module_under_test')

nose 有什么方法可以从目录中查找测试(例如 -w 选项提供的 BUT 保持工作目录与调用nose 的终端的工作目录相同?

【问题讨论】:

    标签: python unit-testing nose


    【解决方案1】:

    哎呀,看起来我需要做的就是将__init__.py 添加到库和子目录测试目录中。我正在假设鼻子会按预期使用 PEP 420

    块引用 PEP 420:隐式命名空间包

    对不需要 init.py 标记文件并且可以自动跨越多个路径段的包目录的本机支持(受各种第三方命名空间包方法的启发,如 PEP 420 中所述)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2011-03-01
      相关资源
      最近更新 更多