How to Automate TOTP Authentication Testing: A Complete Guide for QA Engineers

Aslam Khan

Aslam Khan

Thumbnail

TOTP (Time-based One-Time Password) is a critical security mechanism that generates temporary codes for two-factor authentication. Unlike SMS verification, TOTP codes are generated locally using a shared secret key and synchronized time, making them more secure against attacks.

For QA engineers, automating TOTP testing is essential for security-critical applications. Financial platforms, cloud services, and enterprise applications rely heavily on TOTP authentication.

TOTP_Image_Hero.jpg

Automating TOTP Testing Implementation

Setup and Secret Key Management

Obtain the TOTP shared secret during test environment setup. This base32-encoded string is available through QR codes or configuration files.

Programmatic Code Generation

Use libraries to generate TOTP codes:

import pyotp totp = pyotp.TOTP('base32secretkey') current_code = totp.now()

This eliminates manual code entry and enables continuous integration testing.

Comprehensive Test Scenarios

Automated tests should cover:

  • Valid code authentication during normal login flows
  • Expired code handling by manipulating test timestamps
  • Invalid code rejection to verify security controls
  • Time window tolerance testing for clock drift
  • Multiple authentication attempts within the same period

Clock Synchronization Strategy

Since TOTP codes depend on precise timing, ensure synchronized clocks between test environment and system under test.

Integration with Test Frameworks

Modern frameworks like Selenium, Playwright, or Cypress can integrate TOTP generation into end-to-end test flows. Topt Robonito out of the box supports integration with all the TOTP platforms. You can read more about it in the Robonito docs

Best Practices

  • Secure secret management in encrypted configuration
  • Environment isolation with dedicated test secrets
  • Time manipulation for edge case testing
  • Parallel testing compatibility

Advanced Considerations

Implement agentic AI-driven test planning to automatically generate comprehensive TOTP test scenarios. AI can identify edge cases and optimize test coverage.

Consider testing backup codes, account recovery flows, and multi-device authentication scenarios.

Automating TOTP authentication testing strengthens application security while enabling efficient continuous integration.

If you are interested try Robonito today to experience the easiest way to automate TOTP authentication scenarios.