【问题标题】:Py.test in eclipse throws AttributeError: module 'pytest' has no attribute 'main'eclipse中的Py.test抛出AttributeError:模块'pytest'没有属性'main'
【发布时间】:2020-07-30 11:35:58
【问题描述】:

我有一个简单的 python 测试

def inc(x):
    return x + 1

def test_inc_pass():
    assert inc(5) == 6
    
def test_inc_fail():    
    assert inc(5) == 5 

当我尝试直接从 eclipse 使用 py.test runner 运行这些测试时,我收到了这个错误:

Traceback (most recent call last):
  File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\runfiles.py", line 273, in <module>
    main()
  File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\runfiles.py", line 265, in main
    return pytest.main(argv)
AttributeError: module 'pytest' has no attribute 'main'

我已经尝试使用以下方法重新安装 pytest:

> pip uninstall pytest
> pip install pytest

但没有任何帮助。有趣的是,当我从 CMD/命令行运行测试时 - 一切正常。

C:\Development\eclipse_workspace\PySandbox\pytest>pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.6.5, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
rootdir: C:\Development\eclipse_workspace\PySandbox\pytest
plugins: pydev-0.1.1
collected 2 items                                                                                                                                                       

test_sample.py .F                                                                                                                                                 [100%]

=============================================================================== FAILURES ===============================================================================
____________________________________________________________________________ test_inc_fail _____________________________________________________________________________

    def test_inc_fail():
>       assert inc(5) == 5
E       assert 6 == 5
E        +  where 6 = inc(5)

test_sample.py:9: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_sample.py::test_inc_fail - assert 6 == 5
===================================================================== 1 failed, 1 passed in 0.14s ======================================================================

我猜想eclipse/pydev 一定有什么东西。 有什么建议/帮助吗?

【问题讨论】:

    标签: python pytest pydev


    【解决方案1】:

    也许您在 PyDev 中配置的解释器与您在命令行中使用的解释器不同?

    -- pytest 肯定应该有pytest.main

    请运行下面的代码(作为常规启动,无需从命令行和 Eclipse 中进行单元测试)并提供在这些情况下的输出(它应该清楚什么是可执行文件以及 @987654322 @ 正在获取)。

    import sys
    
    def inc(x):
        return x + 1
    
    def test_inc_pass():
        assert inc(5) == 6
    
    def test_inc_fail():
        assert inc(5) == 5
    
    if __name__ == '__main__':
        import pytest
        print('Running in: %s' % (sys.executable,))
        print('PyTest found in: %s' % (pytest.__file__,))
        pytest.main(sys.argv)
    
    

    【讨论】:

      【解决方案2】:

      奇怪的东西。第一次,当我从 eclipse 运行你的代码时,我遇到了与以前相同的错误,但在重新启动 eclipse 后,我再次运行它并且你的代码工作正常:

      Running in: C:\MojeApps\Python365\python.exe
      PyTest found in: C:\MojeApps\Python365\lib\site-packages\pytest\__init__.py
      ============================= test session starts =============================
      platform win32 -- Python 3.6.5, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
      rootdir: C:\Development\eclipse_workspace\PySandbox\pytest_so_help
      collected 2 items
      
      main.py .F                                                               [100%]
      
      ================================== FAILURES ===================================
      ________________________________ test_inc_fail ________________________________
      
          def test_inc_fail():
      >       assert inc(5) == 5
      E       assert 6 == 5
      E        +  where 6 = inc(5)
      
      main.py:10: AssertionError
      =========================== short test summary info ===========================
      FAILED main.py::test_inc_fail - assert 6 == 5
      ========================= 1 failed, 1 passed in 0.03s =========================
      

      通过 Eclipse 运行“我的测试”给了我新的错误 :)

      ============================= test session starts =============================
      platform win32 -- Python 3.6.5, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
      rootdir: C:\Development\eclipse_workspace\MyPyTests\src
      collected 2 items
      
      tests\test_sample.py .F
      INTERNALERROR> Traceback (most recent call last):
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 240, in wrap_session
      INTERNALERROR>     session.exitstatus = doit(config, session) or 0
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 296, in _main
      INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
      INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
      INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
      INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
      INTERNALERROR>     return outcome.get_result()
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
      INTERNALERROR>     raise ex[1].with_traceback(ex[2])
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
      INTERNALERROR>     res = hook_impl.function(*args)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 321, in pytest_runtestloop
      INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
      INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
      INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
      INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
      INTERNALERROR>     return outcome.get_result()
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
      INTERNALERROR>     raise ex[1].with_traceback(ex[2])
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
      INTERNALERROR>     res = hook_impl.function(*args)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 100, in pytest_runtest_protocol
      INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 117, in runtestprotocol
      INTERNALERROR>     reports.append(call_and_report(item, "call", log))
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 211, in call_and_report
      INTERNALERROR>     hook.pytest_runtest_logreport(report=report)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
      INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
      INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
      INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
      INTERNALERROR>     return outcome.get_result()
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
      INTERNALERROR>     raise ex[1].with_traceback(ex[2])
      INTERNALERROR>   File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
      INTERNALERROR>     res = hook_impl.function(*args)
      INTERNALERROR>   File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydev_runfiles\pydev_runfiles_pytest2.py", line 265, in pytest_runtest_logreport
      INTERNALERROR>     exc = _get_error_contents_from_report(report)
      INTERNALERROR>   File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydev_runfiles\pydev_runfiles_pytest2.py", line 167, in _get_error_contents_from_report
      INTERNALERROR>     tw = TerminalWriter(stringio=True)
      INTERNALERROR> TypeError: __init__() got an unexpected keyword argument 'stringio'
      
      ========================= 1 failed, 1 passed in 0.05s =========================
      

      我开始认为 pydev 插件坏了...

      编辑: 奇怪的事情。在我启动操作系统的那晚之后,检查它是否仍然出现 - 它突然开始工作而没有问题 - 我不知道......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-10
        • 2017-12-05
        • 1970-01-01
        • 2021-01-07
        相关资源
        最近更新 更多