How do I pass an environment variable into a foundry test file?

Viewed 53

Looking to pass an alchemy rpc endpoint url that contains its api key and is stored as an environment variable in a solidity test file executed using forge test. I want to do so in order to use it to fork goerli testnet and potentially be able to manage different forks in the same test context. The vm cheatcode below creates a local fork of goerli at block_number:
vm.createSelectFork($GOERLI_ALCHEMY_SECRET_URL, block_number);
How do I pass it $GOERLI_ALCHEMY_SECRET_URL from the environment?

1 Answers

In order to pass an environment variable into a test file you can make use of vm.envString():

vm.createSelectFork(vm.envString("GOERLI_ALCHEMY_SECRET_URL"), block_number);

Related