Diagnostic testing is a key tool in the fight against many infectious diseases. The emergence of pathogen variants that are able to avoid detection by diagnostic testing therefore represents a key challenge for public health. In recent years, variants for multiple pathogens have emerged which escape diagnostic testing, including mutations in Plasmodium falciparum (malaria), Chlamydia trachomatis (chlamydia) and SARS-CoV-2 (COVID-19). However, little is currently known about when and the extent to which diagnostic test escape will evolve. Here we use a mathematical model to explore how the frequency of diagnostic testing, combined with variation in compliance and efficacy of quarantining, together drive the evolution of detection avoidance. We derive key thresholds under which a testing regime will (i) select for diagnostic test avoidance, or (ii) drive the pathogen extinct. Crucially, we show that imperfect compliance with diagnostic testing regimes can have marked effects on selection for detection avoidance, and consequently, for disease control. Yet somewhat counterintuitively, we find that an intermediate level of testing can select for the highest level of detection avoidance. Our results, combined with evidence from various pathogens, demonstrate that the evolution of diagnostic testing avoidance should be carefully considered when designing diagnostic testing regimes.