我有一個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