【问题标题】:Test script run_test.py fails in conda build of pyfftw测试脚本 run_test.py 在 pyfftw 的 conda 构建中失败
【发布时间】:2014-06-03 21:20:03
【问题描述】:

我已经使用 conda build 成功构建了 fftw 和 pyfftw 的包。我现在正在尝试编写一个 run_test.py 例程来在 pyfftw 中执行测试,作为构建过程的一部分。我写了一个 run_test.py 来运行 pyfftw 中的一个测试文件:

import os
path=os.environ.get('SRC_DIR')
execfile(path+'/test/test_pyfftw_real_backward.py')

在构建结束时,我收到以下错误:

Traceback (most recent call last):
  File "/data/miniconda/conda-bld/test-tmp_dir/run_test.py", line 38, in <module>
    execfile(path+'/test/test_pyfftw_real_backward.py')
  File "/data/miniconda/conda-bld/work/pyFFTW-0.9.2/test/test_pyfftw_real_backward.py", line 24, in <module>
    from .test_pyfftw_base import run_test_suites
ValueError: Attempted relative import in non-package
TESTS FAILED: pyfftw-0.9.2-np18py27_0

我是否将文件添加到包中?或者也许将文件复制到测试环境?我实际上不知道如何进行。任何帮助将不胜感激。

【问题讨论】:

  • 只在run_test.sh中使用python test/test_pyfftw_real_backward.py是否有效?
  • 当我尝试你的建议时,我得到了错误:[Errno 2] No such file or directory。我想知道测试文件是否需要在 envs/_test 或 pkgs 目录中才能工作。
  • 不,您应该可以在任何地方运行测试。

标签: conda


【解决方案1】:

这里有几件事(这个问题是很久以前提出的)。

上面的问题是您无法通过from .something import ... 进行相对导入 - 不是因为该文件不存在,而是因为您将其作为文件运行,类似于:

python test/my_test.py

如果您执行以下操作,它会起作用:

python -m test.my_test

但是,execfile 不是那样的,而且 - 它已经过去了:What is an alternative to execfile in Python 3?

接下来的事情,对于不小心来到这里的人 - 你不应该那样使用SRC_DIR,否则你会得到:

ValueError: In conda-build 2.1+, the work dir is removed by default before 
the test scripts run.  You are using the SRC_DIR variable in your test script,
but these files have been deleted.  Please see the  documentation regarding
the test/source_files meta.yaml section, or pass the --no-remove-work-dir flag.

正确的方法 有关为 conda 包配置测试的更多信息,请参见此处:https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#test-section

所以,你应该让你的测试独立,或者至少他们应该做这样的事情:

import os
import sys

sys.path.append(os.path.dirname(__file__))
import test_common
# etc.

要在源中设置test/ 子目录的工作设置,只需使用:

test:
  requires:
   - nose
  source_files:
   - test/*.py
  commands:
   - nosetests

不用担心这种配置的不足——更少的代码就是更少的维护。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2017-02-08
    相关资源
    最近更新 更多