There are a few different ways to take urine samples, depending on what you need to test for. When veterinarians want to test for certain kinds of bacteria in the urine, they can perform a cystocentesis, which involves inserting a needle directly into the bladder and removing a small amount of urine. This gives the veterinarian a sample of urine that isn't contaminated by any material in the urethra or the outside air.
Veterinarians can also insert a catheter into the bladder to get urine samples.
If you're performing a urine test at home, such as a taking glucose level for a diabetic dog, or if you need to collect urine at home to take into the veterinary clinic, you can do it without making a mess or upsetting your dog. Attach a light, thoroughly cleaned container, like an empty margarine tub or an old Styrofoam egg container, to the end of a yardstick. Keep your dog on a leash when you take him out, so he can't get too far away from you. When he starts to urinate, wait a second or two for impurities to be flushed out, then slip the container into the urine stream. Pour the urine into a clean jar that seals tightly.