Some options:

- Use a [**SMS api service**][1] and let the code retrieve the SMS text from that service.

- Probably you do not want to test the TwoFactorAuth, but you want to test the application behind it. Consider building a **backdoor** which is only enabled in the test environment, so that you can bypass the TFA.

- I have read that people try to create an **Appium** test to retrieve the OTP so that could be another path: https://discuss.appium.io/t/how-to-read-otp-from-messages-using-appium/16949/8


Overall this is pretty challenging and makes the test setup complex and contain dependecies, there for I would go for the **backdoor** option.

  [1]: https://stackoverflow.com/questions/16057903/api-services-for-receiving-sms-text-messaging-online