Dynamically control order of tests with pytest

China☆狼群 提交于 2019-12-05 15:56:51

Not really an full answer to your problem, but to your question on pytest, and few hints.

To change the order of the tests in pytest (just pytest, not pytest-xdist), you can just alter the order of the test items on the go by installing this hook wrapper:

conftest.py:

import pytest
import random

random.seed()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_protocol(item, nextitem):
    yield

    idx = item.session.items.index(item)
    remaining = item.session.items[idx+1:]
    random.shuffle(remaining)
    item.session.items[idx+1:] = remaining

It makes no sense to change what was already executed, but only what remains — hence, [idx+1:]. In this example, I just shuffle the items, but you can do whatever you want with the list of the remaining features.

Keep in mind: This can affect how the fixtures are setup'ed & teardown'ed (those of scope above 'function'). Originally, pytest orders the tests so they can utilise the fixtures in a most efficient way. And specifically, the nextitem argument is used internally to track if the fixture should be finished. Since you efficiently change it every time, the effects can be unpredictable.


With pytest-xdist, it all goes totally different. You have to read on how pytest-dist distributes the tests across the slaves.

First, every slave collects all the same tests and exactly in the same order. If the order is different, pytest-xdist will fail. So you cannot reorder them on collection.

Second, the master process sends the test indexes in that collected list as the next tests to execute. So, the collection must be unchanged all the time.

Third, you can redefine the pytest_xdist_make_scheduler hook. There are few sample implementations in the pytest-xdist itself. You can define your own logic of scheduling the tests in schedule() method, using the nodes added/removed and tests collected via the corresponding methods.

Fourth, that would be too easy. The .schedule() method is called only in slave_collectionfinish event sent from the slave. I'm sad to say this, but you will have to kill and restart the slave processes all the time to trigger this event, and to re-schedule the remaining tests.

As you can see, the pytest-xdist's implementation will be huge & complex. But I hope this will give you some hints where to look at.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!