How to parametrize skipif

Question:

I currently have a set of tests where I want to perform a test on as many GPUs as there are on the host machine. E.G if the machine has 3 GPUS I want to test the function using 1 GPU, 2 GPUs and 3 GPUs

My current approach (which I know is not elegant or efficient) is to have 8 tests, each one configured to use a number of GPUs ranging from 1 to 8, and each one to be skipped if the host machine doesn’t have the required number of GPUs

Is there any way to write only one test where I use @pytest.mark.parametrize to set up 8 different values to be used as number of GPUs within the test and combine that with the skipif fixture to skip tests depending on the gpus of the machine?

Thanks in advance

Asked By: malfonsoarquimea

||

Answers:

Is there any way to write only one test where I use @pytest.mark.parametrize to set up 8 different values to be used as number of GPUs within the test and combine that with the skipif fixture to skip tests depending on the gpus of the machine?

That seems like an overcomplication?

Why not just check the number of GPUs at the toplevel and create a parametrization which goes from one to that number? e.g.

GPU_COUNT = get_number_of_gpus()

@pytest.mark.parametrize('gpus', range(GPU_COUNT))
def test_thing_gpu(gpus):
    ...

That aside skipif is a static condition, it executes during the evaluation of the module itself and takes a boolean condition not a function.

If you want to skip based on a runtime condition, you need to call pytest.skip from inside the test function.

Answered By: Masklinn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.