使用pytest教程出错

Error using pytest tutorial

我读到单元测试是一个出色的功能,可以编写更好的代码并断言某些目标代码的功能保持不变。所以我想用它...

我在 Linux 机器上使用 Anaconda。

我通过阅读手册的入门指南 on their homepage 开始使用 pytest。 成功安装后出现第一个(意外的)错误:

strpeter@linuxComputer:~$ py.test 
================================================== test session starts ===================================================
platform linux2 -- Python 2.7.8 -- py-1.4.25 -- pytest-2.6.3
collected 0 items / 1 errors 

========================================================= ERRORS =========================================================
___________________________________________________ ERROR collecting . ___________________________________________________
anaconda/lib/python2.7/site-packages/py/_path/common.py:331: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
anaconda/lib/python2.7/site-packages/py/_path/common.py:377: in gen
    for p in self.gen(subdir):
anaconda/lib/python2.7/site-packages/py/_path/common.py:377: in gen
    for p in self.gen(subdir):
anaconda/lib/python2.7/site-packages/py/_path/common.py:377: in gen
    for p in self.gen(subdir):
anaconda/lib/python2.7/site-packages/py/_path/common.py:377: in gen
    for p in self.gen(subdir):
anaconda/lib/python2.7/site-packages/py/_path/common.py:367: in gen
    if p.check(dir=1) and (rec is None or rec(p))])
anaconda/lib/python2.7/site-packages/_pytest/main.py:628: in _recurse
    ihook.pytest_collect_directory(path=path, parent=self)
anaconda/lib/python2.7/site-packages/_pytest/main.py:166: in call_matching_hooks
    plugins = self.config._getmatchingplugins(self.fspath)
anaconda/lib/python2.7/site-packages/_pytest/config.py:688: in _getmatchingplugins
    plugins += self._conftest.getconftestmodules(fspath)
anaconda/lib/python2.7/site-packages/_pytest/config.py:521: in getconftestmodules
    mod = self.importconftest(conftestpath)
anaconda/lib/python2.7/site-packages/_pytest/config.py:554: in importconftest
    self._onimport(mod)
anaconda/lib/python2.7/site-packages/_pytest/config.py:674: in _onimportconftest
    self.pluginmanager.consider_conftest(conftestmodule)
anaconda/lib/python2.7/site-packages/_pytest/core.py:201: in consider_conftest
    if self.register(conftestmodule, name=conftestmodule.__file__):
anaconda/lib/python2.7/site-packages/_pytest/core.py:100: in register
    reg(plugin, name)
anaconda/lib/python2.7/site-packages/_pytest/config.py:613: in _register_plugin
    call_plugin(plugin, "pytest_configure", {'config': self})
anaconda/lib/python2.7/site-packages/_pytest/core.py:265: in call_plugin
    kwargs=kwargs, firstresult=True).execute()
anaconda/lib/python2.7/site-packages/_pytest/core.py:315: in execute
    res = method(**kwargs)
anaconda/lib/python2.7/site-packages/astropy/tests/pytest_plugins.py:78: in pytest_configure
    if not config.getoption('remote_data'):
anaconda/lib/python2.7/site-packages/_pytest/config.py:836: in getoption
    raise ValueError("no option named %r" % (name,))
E   ValueError: no option named u'remote_data'
                                                     DO *NOT* COMMIT!                                                     
================================================ 1 error in 2.77 seconds =================================================

我想知道这个愚蠢的错误是从哪里来的,我该如何解决它。 问题是我在没有任何文件名的情况下执行程序 py.test 并且没有名为 __init.py__ 的文件吗? 好吧,我问这个问题真的很愚蠢,但请认真对待这个问题,因为我在万维网上没有发现任何提示。

我刚遇到这个问题。当您 运行 从错误的目录进行 pytest 时会出现这种情况,例如安装后无论您身在何处。

求解,

  • 创建一个新目录并进入该目录
  • 在您的目录中创建文件 test_sample.py,内容按照 pytest tutorial
  • 现在 运行 命令 pytest 您将看到教程中显示的结果。

我自己对此并不陌生,但我相信 pytest 会从 运行 寻找测试的位置搜索目录结构。 运行 它来自错误的地方,如果它看到它认为可能是测试的东西,你会得到一些奇怪的结果。