简体   繁体   中英

PyDev running pytest unit test with module-shared fixture fails

I have a problem running pytest unit tests with pyDev. I try to run a unit test with a module shared fixture and a finalizer which should be excecuted after the last test. But when running the unit test in pyDev it does not use the same instance but instead creates two different instances. The example is running fine in the console or when started from a script within pydev.

I'm using platform Python 2.7.3, pytest-2.3.4, pyDev 2.7.3.2013031601, Eclipse 4.2 on Win7.

I tried the example from http://pytest.org/latest/fixture.html

The output from pydev is:

============================= 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>

The console output is:

============================= 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>

Which is the expected behaviour. What am I doing wrong??

the used code is conftest.py:

import pytest
import smtplib

@pytest.fixture(scope="module")
def smtp():
return smtplib.SMTP("merlinux.eu")

The test code in 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

Running the test from script with:

import pytest,os
os.chdir("[path_to_tests]/tests") #your file location
pytest.main(['-s', 'test_smtplib.py'])

Any suggestions and thanks a lot for your help!

I don't have eclipse but I've been looking through the source code for Pydev and pytest. pytest doesn't use multiprocessing by default but it will if you have xdist installed. Perhaps you have it? Or perhaps Eclipse has installed it?

If you still have the system available, can you try setting the option below in your pytest parameters? It simply tells pytest to use one process when using xdist as documented here .

-n=1 or perhaps it will prefer -n 1

If that doesn't work, then this also shouldn't work but could you try it? Use the option below in the pytest options as before (not in the pydev test runner options) to enable module-level testing. It's a pydev test runner option so I guess it will cause an error, but maybe some other code that keys off the option will make use of it.

--split_jobs=module or again perhaps --split_jobs module

Seems like this is a long-standing bug on the pydev side. I just fixed it and submitted a pull request to Pydev, see https://github.com/fabioz/Pydev/pull/120 . In the meanwhile, you could probably take out the little change and apply to your installed version of pydev, enabling proper pydev/pytest runs with scoping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM