The question of whether community nucleic acid testing contributes to an increase in infections within residential compounds has not been definitively answered. Shanghai, one of the largest cities in China, conducted city-wide community testing during its lockdown from late March to May 2022. This situation provided a unique opportunity to examine the effect of community testing on infection rates, as the lockdown largely eliminated confounding factors such as citizen mobility. In our study, based on a survey of 208 residential compounds in Shanghai and the daily infection data during the lockdown period, we found a significant correlation between community testing and infection risk in these compounds. However, after addressing potential issues of reverse causality and sampling bias, we found no significant causal link between community testing and infection risk. Furthermore, we discovered that increased awareness of mask-wearing correlated with a decrease in infections within the residential compounds during community testing. This suggests that the perceived correlation between community testing and infection risk may be confounded by residents’ adherence to mask-wearing practices. Our findings emphasize the need for public health decision-makers to reinforce the importance of mask-wearing during community testing, as a means to prevent infections among citizens.