# Do not set --prerunmodifier on each pabot thread

**URL:** <https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148>\
**Category:** Pabot\
**Created:** [10 December 2024 23:21 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148 "2024-12-10T23:21:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilsoft-gt](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/wilsoft-gt/32/2656_2.png) [@wilsoft-gt](https://forum.robotframework.org/u/wilsoft-gt)\
**Post date:** [10 December 2024 23:21 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/1 "2024-12-10T23:21:54Z")

</div>

I’m using a prebuild modifier to create multiple copies of a test case and passing some data to them, my issue is that pabot is calling --prerunmodifier for each run. Is there any way i can create the test cases but make pabot to not add the --prerunmodifier flag? As an example this is the code pabot execute for each test case:

> robot --test ‘Regresivo.TC004 Generar links\_codigo\_6’ --variable openBrowser:False --variable CALLER\_ID:7146a479c84e4b68b109e5e4e1f636c6 --variable PABOTLIBURI:127.0.0.1:8270 --variable PABOTEXECUTIONPOOLID:6 --variable PABOTISLASTEXECUTIONINPOOL:0 --variable PABOTNUMBEROFPROCESSES:10 --variable PABOTQUEUEINDEX:6 --variable ‘PABOTLASTLEVEL:Regresivo.TC004 Generar links\_codigo\_6’ --outputdir /tmp/pabot\_results/6 --listener RobotStackTracer **–prerunmodifier /tmp/PreRunModifier.py** --log NONE --report NONE --xunit NONE --consolecolors off --consolemarkers off /tmp/claro/TestCases/Regresivo.robot

I’m open to other solutions, datadriven does not work for me because the amount of test cases I need to add dinamically depend on a dinamically generated list.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [10 December 2024 23:47 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/2 "2024-12-10T23:47:44Z")

</div>

Hi Wilson,

If everything is running on the same machine a simple “lock file” approach could be used, the first instance of `PreRunModifier.py` creates the lock file and the others on detecting it exit as it’s already been run, you could also add a step to your suite teardown to remove the lock file if you want.

Another approach would be to put the `PreRunModifier.py` content into a keyword you call from RF, add it to your suite startup with pabot’s keyword [Run Setup Only Once](https://pabot.org/PabotLib.html)

These are the simplest suggestions I have, but if your environment is more complex you might need another approach.

Hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![wilsoft-gt](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/wilsoft-gt/32/2656_2.png) [@wilsoft-gt](https://forum.robotframework.org/u/wilsoft-gt)\
**Post date:** [11 December 2024 05:05 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/3 "2024-12-11T05:05:43Z")

</div>

Thank you for your reply, this is what I have so far in my prerunmodifier:

```
def visit_test(self, data):
    parser = ParseVariablesLibrary()
    values = parser.parse_data("file", "name")
    test_cases = []
    if "TC004" in data.tags:
        for brand in values:
        #print(f"Test case nuevo agregado: {testcase.body}")
            new_test = data.copy(name=f"{data.name}_{marca['marca']}")
            new_test.body.insert(0, new_test.body.create_keyword(name="Set Test Variable", args=["@{variable}",brand]))
            self.suite_test_cases.append(new_test)
    else:
        self.suite_test_cases.append(data)
    self.visited = True

```

The idea is that if the test case has a TC004 tag, it will create copies of the same test case with a variable named “${variable}” for each “brand”, and it does it perfect if running with robot.

My issue is that when I run it with pabot, the prerunmodifier is being called on each new thread and if I have 10 items inside values, the dinamic generated test cases are going to have 10 times the variable “${variable}”.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [11 December 2024 05:26 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/4 "2024-12-11T05:26:25Z")

</div>

Hi Wilson,

I have no experience with using prerunmodifiers, so I have no idea how pabot will handle the test cases, so you’ll have to try it and see.

As I mentioned you can create a lock file to stop the prerunmodifier appending the TC004 tests multiple times, it would look something like this (I’ll make some minor modification to your code):

```python
import os

def visit_test(self, data):
    parser = ParseVariablesLibrary()
    values = parser.parse_data("Datos", "Crear Equipos")
    test_cases = []
    if "TC004" in data.tags:
        if not os.path.exists("TC004.lock"):
            with open(path, "w+") as f:
                f.write("TC004") 
            for brand in values:
                #print(f"Test case nuevo agregado: {testcase.body}")
                new_test = data.copy(name=f"{data.name}_{marca['marca']}")
                new_test.body.insert(0, new_test.body.create_keyword(name="Set Test Variable", args=["@{variable}",brand]))
                self.suite_test_cases.append(new_test)
    else:
        self.suite_test_cases.append(data)
    self.visited = True

```

So this will run as it does now, but will check for the file `TC004.lock` if it’s found it does nothing, if it’s not found it creates it first and then creates the test cases as before

see if that works for you,

Dave.

---

<div class="post-metadata">

**Author:** ![mkorpela](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mkorpela/32/33_2.png) [@mkorpela](https://forum.robotframework.org/u/mkorpela)\
**Post date:** [11 December 2024 06:04 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/5 "2024-12-11T06:04:33Z")

</div>

I’m open to changing pre-run modifiers so that they would be ran only once in pabot main process.  
This would be a backward incompatible change and have to say that I have no way of understanding the potential negative impacts.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [11 December 2024 06:39 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/6 "2024-12-11T06:39:58Z")

</div>

Hi Mikko,

Some ideas rather than having a breaking change:

- A shared variable area that the pre-run modifiers can access to set / read a variable, then people can manage the “run once” control themselves with a if statement.
- A function that does something similar to `Run Setup Only Once`.
- A `run_once` decorator for functions like `visit_test`

I’ll leave it to you to decide what’s best for pabot

Dave.

---

<div class="post-metadata">

**Author:** ![wilsoft-gt](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/wilsoft-gt/32/2656_2.png) [@wilsoft-gt](https://forum.robotframework.org/u/wilsoft-gt)\
**Post date:** [11 December 2024 18:27 UTC](https://forum.robotframework.org/t/do-not-set-prerunmodifier-on-each-pabot-thread/8148/7 "2024-12-11T18:27:38Z")

</div>

I solved my issue, the issue was the following code:

`new_test = data.copy(name=f"{data.name}_{marca['marca']}")`

It seems that instead of creating a copy of the testcase, it was creating a copy with a different name but referencing the original test case, so every time I called the following:

`new_test.body.insert(0, new_test.body.create_keyword(name="Set Test Variable", args=["@{variable}",brand]))`

The original test case with tag TC004 also received the insertion of the keyword, so If I created 10 copies, the Set Test Variable was inserted 10 times in the original TC also. I imported the copy library and use the deepcopy funcion. That was the trick.
