It is now possible to set up sandbox codes for testing in the sandbox environment. See the 'Create sandbox codes' section from Apple's documentation.
https://developer.apple.com/help/app-store-connect/manage-subscriptions/set-up-offer-codes/