Python nosetest不起作用,但是使用matplotlib的@image_comparison直接运行它

时间:2014-02-24 21:30:10

标签: python matplotlib nose nosetests

我有一个名为test_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运行它,那么这个奇怪的IndexErrormatplotlib的{​​{1}}装饰器代码为中心就会失败:

@image_comparison

可能会发生什么想法?

1 个答案:

答案 0 :(得分:1)

我认为问题在于装饰器希望在子目录中使用主项目目录的“测试”(通常是project/tests或更正确:测试模块必须以{{1}开头})。这是something.tests.中的代码:

_image_directories()

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) _image_directories()的情况下有一些特殊代码,因此您在第一种情况下看不到错误。