我有一个pyDev运行pytest单元测试问题。我尝试使用模块共享夹具和终结器进行单元测试,这应该在最后一次测试后执行。 但是,在pyDev中运行单元测试时,它不使用相同的实例,而是创建两个不同的实例。该示例在控制台中运行正常,或者从pydev中的脚本启动时运行良好。PyDev运行与模块共享夹具失败pytest单元测试
我在Win7上使用平台Python 2.7.3,pytest-2.3.4,pyDev 2.7.3.2013031601,Eclipse 4.2。
我试图从http://pytest.org/latest/fixture.html
输出从PyDev的例子是:
============================= test session starts ==============================
platform win32 -- Python 2.7.3 -- pytest-2.3.4
__________________________________ test_ehlo ___________________________________
smtp = <smtplib.SMTP instance at 0x027F9080>
__________________________________ test_noop ___________________________________
smtp = <smtplib.SMTP instance at 0x027FF3C8>
控制台输出为:
============================= test session starts ==============================
platform win32 -- Python 2.7.3 -- pytest-2.3.4
__________________________________ test_ehlo ___________________________________
smtp = <smtplib.SMTP instance at 0x01E51288>
__________________________________ test_noop ___________________________________
smtp = <smtplib.SMTP instance at 0x01E51288>
这是预期的行为。我究竟做错了什么??
所使用的代码是conftest.py:
# content of test_module.py
def test_ehlo(smtp):
response = smtp.ehlo()
assert response[0] == 250
assert "merlinux" in response[1]
assert 0 # for demo purposes
def test_noop(smtp):
response = smtp.noop()
assert response[0] == 250
assert 0 # for demo purposes
从脚本运行测试:
import pytest,os
os.chdir("[path_to_tests]/tests") #your file location
pytest.main(['-s', 'test_smtplib.py'])
任何建议
import pytest
import smtplib
@pytest.fixture(scope="module")
def smtp():
return smtplib.SMTP("merlinux.eu")
在test_smtplib.py测试代码并感谢您的帮助!
您是否配置PyDev IDE以使用** py.test **测试运行器(* Window - > Preferences - > PyDev - > PyUnit - > Test Runner *)? – 2013-04-08 10:08:28
是的,选择PyDev Test Runner。 – MBaumann 2013-04-08 12:02:01
不,不是* PyDev Test Runner *,你应该也可以选择*** Py.test ** Test Runner * - >看看:http://up.picr.de/14054750vh.jpg – 2013-04-08 12:49:10