It sounds like the set a variable method would work for you, as long as the LMS controls access to the course. What I mean is if you only use a per session variable it will not prevent students from reentering the course and taking the test again.
In some LMS systems you can set a period of enrollment after which the student will be un-enrolled. That might be another way to control retesting, maybe after a certain period they have to enroll in the course again and at that would clear the variable allow them to retake the test.
Hopefully this give you some further insight and a possible solution.