首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python nosetest不能工作,但是可以使用matplotlib的@image_comparison直接运行它

Python nosetest不能工作,但是可以使用matplotlib的@image_comparison直接运行它
EN

Stack Overflow用户
提问于 2014-02-25 05:30:10
回答 1查看 700关注 0票数 2

我有一个名为test_boxplot.py的测试脚本:

代码语言:javascript
复制
__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'])

如果我直接运行它,测试都会通过:

代码语言:javascript
复制
$ 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运行它,它会失败,出现这个奇怪的IndexError,它以matplotlib@image_comparison装饰器的代码为中心:

代码语言:javascript
复制
$ 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)

你知道可能发生了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2014-02-25 05:46:19

我认为问题是装饰器期望在主项目目录的子目录中使用“something.tests.”(通常是测试或更准确地说:测试模块必须以test开头)。这是_image_directories()中的代码

代码语言:javascript
复制
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)

func.__module__ == '__main__'的情况下,_image_directories()有一些特殊的代码,所以您在第一种情况下看不到错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21999433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档