I have a code that should work on both gpu and cpu. Therefore, I write a test (pytest) to test the scenario of a device with cpu only or with gpu disabled.
I tried to modify the CUDA_VISIBLE_DEVICES environment variables but it does not work out.
def test_training_without_gpu(config):
environ_buffer = dict(environ)
    environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    environ['CUDA_VISIBLE_DEVICES'] = ''  # hide all CUDA devices

    environ['Test'] = 'False'

    assert not cuda.is_available()
    training(config)

    # recover the env variables
    environ.clear()
    environ.update(environ_buffer)

Would anyone please help?