They test before and after competitions. The Belarussian who got her gold medal taken from her was tested immediately before and after her competition but results weren't available immediately -- both tests came up positive. The medal awards are done immediately after results are known -- which may be before tests are finished. The IOC hopes the fact of the rigorous testing is enough to prevent doping during competition (and preferably at all) but the Belarussian must have thought she was smarter than them.

They don't really have to pull medals that often -- you see competitors disqualified before competition more often than post-win pulls and a lot of the post-win pulls come after other evidence comes out like public testimony.