In addition to fgrieu's correct answer, even if you don't know the factorization of $p-1$, you can still get an answer that has a high probability of being correct (assuming that the value you're testing $g$ was chosen randomly and not maliciously).
Here's how you do it: you obtain a partial factorization of $p-1$, finding all small prime factors $q_0 = 2, q_1, q_2, ...$ which are less than $\lambda$, that is $p-1 = q_0 q_1 q_2 ... q_n C$ where $C$ has no factors $< \lambda$ (and is presumably composite - if it is prime or one, you have a complete factorization and can use fgrieu answer, which always gives you the right answer). The Elliptic Curve Method of factorization can give you such a partial factorization with $\lambda \approx 2^{128}$ being practical, and even larger $\lambda$s are possible (depending on your computing resources).
Once you have that, you compute (just as fgrieu stated) $g^{(p-1)/q_i} \bmod p$, and if they are all something other than 1, then you have a generator, with a maximum probability of being wrong (assuming $g$ was chosen randomly) $< \frac{\log p} {\lambda \log \lambda}$ - for $\lambda$ in the range that ECM allows you, this may be adequately small...