pytest-dev / pytest-dev/pytest-xdist

How to access CLI parameters when running with xdist

Open
#670 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.9k
Forks
287
Avg merge
9h 30m
Merged PRs (30d)
2

Description

Hello, so i've been using sys.argv to know wich environment the tests should run form the CLI and depending on the environment the data the tests use.

tests/data.py

def env():
if "--env=DEV" in sys.argv:
    return 'DEV'
elif "--env=UAT" in sys.argv:
    return 'UAT'
elif "--env=PROD" in sys.argv:
    return 'PROD'
elif "--env=TE" in sys.argv:
    return 'TE'
elif "--env=PERF" in sys.argv:
    return 'PERF'
else:
    return 'Incorrect environment'




if env() == 'UAT':
     policy_list_fnol_ap = ['30-60000550', '30-60000551', '30-60000555', '30-60000727', '30-60000798', '30-60000549']
     policy_list_fnol_auto = ['4-65000343', '44-45000075', '44-45000076', '4-65000344']
     policy_list_fnol_vida = ['23-54000022', '23-54000023', '17-55000042']
elif env() == 'DEV':
     policy_list_fnol_ap = ['30-60000301', '30-60000302', '30-60000303', '30-60000304', '30-60000305', '30-60000300']
     policy_list_fnol_auto = ['4-65000178', '44-45000030', '4-65000179', '44-45000031']
     policy_list_fnol_vida = ['23-54000020', '23-54000021', '17-55000018']

If i run this ussing "-n" parameter, sys.argv returns "-c" parameter so i can't get the environment from CLI with -n.
I was reading the documentation:

Accessing sys.argv from the master node in workers
To access the sys.argv passed to the command-line of the master node, use request.config.workerinput["mainargv"].

I'm not sure how to implement this. My problem is that this code isn't inside the tests or conftest.py, is just a data.py file that defines the data and then the data is used as paramteres:

policy_list = tests.data.policy_list_fnol_ap
@pytest.mark.parametrize('policy_number', policy_list)
def test_fnol_delete_draft:
    script....

Is there a way i can access the CLI parameters outside the tests? With sys.argv i'm able to do it, but sys.argv doesn't work with xdist since the response is "-c" and not the full CLI.

Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented “Accessing sys.argv from the master node in workers” guidance and the issue’s tests/data.py example. Determine how data.py can receive the original CLI environment when xdist is enabled, then verify that the selected environment produces the intended parametrized policy lists under a distributed test run.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.