我有一个简单的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运行器运行那些测试时,我得到了这个错误:
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一定有东西。 有任何建议/帮助吗?
答案 0 :(得分:1)
也许您在PyDev中配置的解释器与您在命令行中使用的解释器不同?
-pytest肯定应该有pytest.main
。
请运行以下代码(作为常规启动运行,而无需从命令行以及在Eclipse中进行单元测试),并提供在这种情况下的输出(应明确可执行文件是什么,{{ 1}}的来源。)
pytest
答案 1 :(得分:0)
奇怪的事情。第一次,当我从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插件坏了...
编辑: 奇怪的事情。晚上,当我启动操作系统后,检查它是否仍然出现-它突然开始正常工作了-我不知道...