Directions are usually to be inactive (no ejaculation) for at least 24 hours before obtaining a sample for a semen analysis test. You will be provided with instructions before the doctor orders the test, typically.
A little more information for you:
A temporary reduction in sperm production (low sperm count) can be caused by many things, such as extreme heat, insomnia, and performance pressure or stress. So if the first test comes back with a low count, it is typically verified with at least two more analyses. At least 2 to 4 weeks are recommended between each analysis.
Eating well, keeping well hydrated and sleeping well will improve test results. Poor nutrition and anemia can negatively affect your sperm count. Of course, smoking tobacco
and recreational drug use can negatively influence the sperm count as well. Overheating of the scrotum (through tight underwear, warm baths or sauna) can reduce production of sperm too.
Let me know if you have any more questions.
Have a great day!