[TIP] Selecting test Item at run time

Bruno Oliveira nicoddemus at gmail.com
Tue Nov 28 04:46:13 PST 2017


Hi,

You will probably need to override the pytest_runtestloop hook. Here’s
pytest’s implementation in main.py:

def pytest_runtestloop(session):
    if (session.testsfailed and
            not session.config.option.continue_on_collection_errors):
        raise session.Interrupted(
            "%d errors during collection" % session.testsfailed)

    if session.config.option.collectonly:
        return True

    for i, item in enumerate(session.items):
        nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
        item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
        if session.shouldfail:
            raise session.Failed(session.shouldfail)
        if session.shouldstop:
            raise session.Interrupted(session.shouldstop)
    return True

You should be able customize the for loop to fit your requirements.

You will need to keep the runtest reports around somewhere if you need to
see if a test item has passed or not, there’s an example on how to do that
here:

http://pytest.readthedocs.io/en/latest/example/simple.html#making-test-result-information-available-in-fixtures

In this example each test item gets a rep_<when> attribute with the report
object.

Hope this helps!

Cheers,
Bruno.

On Mon, Nov 27, 2017 at 2:21 PM arun kali raja arunsep886 at gmail.com
<http://mailto:arunsep886@gmail.com> wrote:

Hi,
>
> Currently pytest creates an item object for each of the testcase and then
> puts them in a list. During run phase it serially pics one item after the
> other and runs the testcases one after the other.
>
> I have a requirement where the next testcase to run has to be chosen
> dynamically based on the pass or fail criteria of the previous testcase..
>
> Say i logically arrange by testcases in a tree structure like below:
>
> [image: image.png]
> ×
> ×
> ×
>
> So if Test_A passed then test_B can be executed or else Test_C has to be
> executed. And so forth..
>
>
> There could be a scenarios:
>
> 1. where say i may have to run a single level for multiple iterations to
> verify if i am getting a stable result at a particular level.
>
> 2. Say through some internal logic i selected Test_H and it has failed. I
> may now decide to run Test_F and see if it passes.
>
> pytest_runtest_teardown And seems to get the nextItem directly so how to
> control what is the nextItem to be executed.Is there any existing hook i
> can use to do this.. Or can i implement a new hook to do such things???
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20171128/c05e28b4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 33470 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20171128/c05e28b4/attachment-0001.png>


More information about the testing-in-python mailing list