[pytest-dev] How to duplicate pytest.Item inside function 'pytest_collection_modifyitems'?

Anton Shiban kantonshiban at gmail.com
Wed Apr 15 12:31:15 EDT 2020


Hi Ronny,
    Thanks a lot for your reply,
     I got your points that pytest does have such support.
     And also I got your points that I can create new Item which is
reasonably equivalent, but I dont know whether the same code may
continuously work for all future release of PYTEST and it may not be a
suggested way to do this.

    Let me explain my requirement.
     1)   I am writing automation test for upgrade of multiple components.
      2)   For this, I have to execute group of tests(using pytest marker)
before executing component-A
     3)   Then I upgrade component-A by executing one testcase. Then again
need to execute same group of test again(using pytest marker). (may be few
more group of test also)
      4)   Then upgrade component-B by executing another testcase. Then
again need to execute the same group of test again (using pytest marker). (
may be few more group of test also)
      I have to repeat this for all available number of components
specified in the configuration file. Configuration file will have details
of what all components to be upgraded and what all testcases (with marker
name) to be executed before and after upgrade of every components.

      So, I planed to write code as explained in below step.
      1)  Run pytest CLI by giving root test path(of all test files). So
that all the tests are collected by pytest.
      2)  Read the configuration file (which has the upgrade plan) from '
pytest_collection_modifyitems'
      3)  Here items list will have all the test Items.
      4)  Identity which tests to be exuected(from the pytest marker) from
the configuration file.
      5)  Filter those tests usign marker name and put it on another items
list (called '*final_items_list*'). This final_items_list will be returned
from this function
      6)  Next, Identify what components to be upgraded from the
configuration file. Identity the testcase name(which does this componet
upgrade) from the items list and put it on the *final_items_list*.
      7)  Identity again which tests to be executed after that component
upgrade. Now filter those tests using marker name and put it into the
*final_items_list*. Here test items are may be same tests which is
collected in step-4.
      8)  I repeat the same till all components specified in the
configuration file.

       Here, the *final_items_list *will have same Items repeated (means
same memory reference of items). So if one item(test) fails, then pytest
will mark  'fail' for all all other same items as well. And also facing
pytest failures when pytest executing same items again. Thats why, I wanted
to clone the Item before adding into '*final_items_list*'.

     Do you have any suggestion to achieve this in pytest?

Thanks a lot,
Shiban.

On Wed, Apr 15, 2020 at 1:55 PM Ronny Pfannschmidt <
opensource at ronnypfannschmidt.de> wrote:

> *copey with the correct mail*
>
> Hi Shiban,
>
> its not safely possible to clone a item in general.
>
> A) you need to make a new nodeid to prevent some issues
>
> B) there is no safe cloning mechanism declared, different subtypes of
> items have different mutable state you dont want to mix
>
> I believe the closest you can get is creating a new item that is
> reasonably equivalent.
> The Details of that would depend on what the actual Item subtype is.
>
> -- Ronny
> Am 15.04.20 um 07:45 schrieb Ronny Pfannschmidt:
>
> Hi Shiban,
>
> its not safely possible to clone a item in general.
>
> A) you need to make a new nodeid to prevent some issues
>
> B) there is no safe cloning mechanism declared, different subtypes of
> items have different mutable state you dont want to mix
>
> I believe the closest you can get is creating a new item that is
> reasonably equivalent.
> The Details of that would depend on what the actual Item subtype is.
>
> -- Ronny
> Am 14.04.20 um 23:46 schrieb Anton Shiban:
>
> Hi,
>
>
> Can you please guide me *how to* *clone pytest.Item?*
>
> I want to take one *item* from *items* array (inside function
> ‘pytest_collection_modifyitems’) and clone that *item* and add it to
> *items* array back.
>
> Can someone please guide to do this?
>
>
> Thanks,
>
> Shiban.
>
> _______________________________________________
> pytest-dev mailing listpytest-dev at python.orghttps://mail.python.org/mailman/listinfo/pytest-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20200415/c8f377ec/attachment.html>


More information about the pytest-dev mailing list