使用 pytest 教程时出错

str*_*ter 5 python unit-testing pytest

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

我在我的 Linux 机器上使用 Anaconda。

我通过阅读他们主页上的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 =================================================
Run Code Online (Sandbox Code Playgroud)

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

Bob*_*byG 6

我刚刚遇到这个问题。当您从错误的目录运行 pytest 时,例如安装后您碰巧在的任何地方,就会出现这种情况。

为了解决,

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

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