Question

I have a simple test.

def test_sample(str):
    print str

And using pytest_generate_tests() e.g.

def pytest_generate_tests(metafunc):
    argnames = [str]
    argvalues = [[Hello], [World]]
    print "Entered pytest_generate_tests"
    metafunc.parametrize(argnames, argvalues)

Now if I use python-xdist to run tests in parallel. I see that pytest_generate_tests() is called "n" number of times depending upon the cmdline option.

py.test -n 2 --> prints "Entered pytest_generate_tests" twice. Is this correct behavior ? I was expecting that only the test will be executed in separate thread.

Env: python 2.7 py.test 2.5.2 pytest-xdist 1.10

Was it helpful?

Solution

Yes, this is correct... pytest_generate_tests is a hook used by py.test during collection phase, which in turn is executed by each slave in order to obtain the tests to execute ("test items"). Which slave executes which test item is than handled by the process that started the test session.

I have not studied all internals fully yet, but I think one of the reasons for this is that you can't send a test item from one process to another because they are not serializable. So the approach is for every slave to do the collection themselves in order to obtain the test items and their "test ids", which then can be exchanged between processes as means of communication, both which slave should execute each test as well as reporting test results back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top