I've wondered that too. Why take something if you know you're going to be tested? Or is it that they are tested at random times throughout the Tour and so Landis (if he is guilty) figured it was worth risking it since he was in a desperate situation at that point in the race? Another possibility is that he thought he would able to beat the system - that he had some way of masking the banned substance but for some reason the mask failed to provide cover for him.
Every winner of a stage is systematically tested. Maybe Landis didn't expected to win the 17th stage...