pyDev で pytest 単体テストを実行する際に問題があります。モジュール共有フィクスチャと、最後のテストの後に実行する必要があるファイナライザーを使用して単体テストを実行しようとしています。ただし、pyDev で単体テストを実行すると、同じインスタンスは使用されず、代わりに 2 つの異なるインスタンスが作成されます。この例は、コンソールで、または pydev 内のスクリプトから開始したときに正常に実行されています。
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 です:
import pytest
import smtplib
@pytest.fixture(scope="module")
def smtp():
return smtplib.SMTP("merlinux.eu")
test_smtplib.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'])
ご提案とご協力に感謝します!