It seems to me that it is impossible to prevent cheating (as in: communicating and getting help from outside) if one allows students to use the restroom during written classroom tests.
Excluding creative solutions involving full-body searches, Faraday cages or invigilators in the stalls, the most practical way to prevent it seems to completely forbid the students to leave the room.
Of course, this seems overly penalizing to students with small health problems or personal emergencies, since they would have to retake the test.
How is this problem dealt with in practice in universities? What is the best solution?