Python-nosetest不';不起作用,但直接使用matplotlib';s@image\u比较

Python-nosetest不';不起作用,但直接使用matplotlib';s@image\u比较,python,matplotlib,nose,nosetests,Python,Matplotlib,Nose,Nosetests,我有一个测试脚本,名为test\u boxplot.py: __author__ = 'olga' from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt import numpy as np import prettyplotlib as ppl import os @image_comparison(baseline_images=['boxplot'], ex

我有一个测试脚本,名为
test\u boxplot.py

__author__ = 'olga'

from matplotlib.testing.decorators import image_comparison
import matplotlib.pyplot as plt
import numpy as np
import prettyplotlib as ppl
import os


@image_comparison(baseline_images=['boxplot'], extensions=['png'])
def test_boxplot():
    # Set the random seed for consistency
    np.random.seed(10)

    data = np.random.randn(8, 4)
    labels = ['A', 'B', 'C', 'D']

    fig, ax = plt.subplots()
    ppl.boxplot(ax, data, xticklabels=labels)
    # fig.savefig('%s/baseline_images/test_boxplot/boxplot.png' %
    #             os.path.dirname(__file__))


if __name__ == '__main__':
    import nose
    nose.runmodule(argv=['-s', '--with-doctest'])
如果我直接运行它,测试都会通过:

$ python test_boxplot.py
/Users/olga/workspace-git/matplotlib/lib/matplotlib/testing/decorators.py:288: UserWarning: test module run as script. guessing baseline image locations
  warnings.warn('test module run as script. guessing baseline image locations')
.
----------------------------------------------------------------------
Ran 1 test in 0.224s

OK
但是如果我用
nosetests
运行它,它会失败,因为这个奇怪的
indexer
围绕
matplotlib
@image\u comparison
装饰器的代码:

$ nosetests test_boxplot.py
E
======================================================================
ERROR: Failure: IndexError (pop from empty list)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/loader.py", line 286, in generate
    for test in g():
  File "/Users/olga/workspace-git/matplotlib/lib/matplotlib/testing/decorators.py", line 145, in test
    baseline_dir, result_dir = _image_directories(self._func)
  File "/Users/olga/workspace-git/matplotlib/lib/matplotlib/testing/decorators.py", line 296, in _image_directories
    assert mods.pop(0) == 'tests'
IndexError: pop from empty list

----------------------------------------------------------------------
Ran 1 test in 0.092s

FAILED (errors=1)

有什么想法吗?

我认为问题在于,装饰程序希望在主项目目录的子目录中使用名称“tests”(通常是
project/tests
或更准确地说:测试模块必须以
something.tests.
开头)。这是
\u image\u目录()中的代码。

mods=module_name.split('.'))

mods.pop(0)#嗯,是的,当我运行它时,我在一个
tests/
目录中:
$pwd/Users/olga/workspace git/prettyplotlib/tests
但是当我在
/Users/olga/workspace git/prettyplotlib
中运行
nosetests
时,我从空列表中得到了相同的
pop
errorNo,我认为您需要从存储库根目录运行测试:查看ggplot测试:->测试位于/ggplot/tests中,nose是从中调用的,因此测试位于“ggplot.tests.test”之类的模块中。如果您愿意,您还可以在中窃取测试代码,这将重新实现装饰器,并使用其他一些方法来计算基线图像dir(第64行及以下)。
mods = module_name.split('.')
mods.pop(0) # <- will be the name of the package being tested (in
            # most cases "matplotlib")
assert mods.pop(0) == 'tests'
subdir = os.path.join(*mods)