pytest-xdist generate random & uniqe ports for each test

539 views Asked by At

I'm using pytest-xdist plugin to run some test using the @pytest.mark.parametrize to run the same test with different parameters.

As part of these tests, I need to open/close web servers and the ports are generated at collection time.

xdist does the test collection on the slave and they are not synchronised, so how can I guarantee uniqueness for the port generation.

I can use the same port for each slave but I don't know how to archive this.

1

There are 1 answers

0
Amir Rossert On BEST ANSWER

I figured that I did not give enough information regarding my issue.

What I did was to create one parameterized test using @pytest.mark.parametrize and before the test, I collect the list of parameters, the collection query a web server and receive a list of "jobs" to process.

Each test contains information on a port that he needs to bind to, do some work and exit because the tests are running in parallel I need to make sure that the ports will be different.

Eventually, I make sure that the job ids will be in the rand on 1024-65000 and used that for the port.