  Let $G$ be a group. An element $g \in G$ is called a test element of $G$ if
for every endomorphism $\varphi:G \to G$, $\varphi(g)=g$ implies that $\varphi$
is an automorphism. We prove that for a finitely generated profinite group $G$,
$g \in G$ is a test element of $G$ if and only if it is not contained in a
proper retract of $G$. Using this result we prove that an endomorphism of a
free pro-$p$ group of finite rank which preserves an automorphic orbit of a
non-trivial element must be an automorphism. We give numerous explicit examples
of test elements in free pro-$p$ groups and Demushkin groups. By relating test
elements in finitely generated residually finite-$p$ Turner groups to test
elements in their pro-$p$ completions, we provide new examples of test elements
in free discrete groups and surface groups. Moreover, we prove that the set of
test elements of a free discrete group of finite rank is dense in the profinite
topology.
