We show how to estimate the enclosed mass from the observed motions of an ensemble of test particles. Traditionally, this problem has been attacked through virial or projected mass estimators. Here, we examine and extend these systematically, and show how to construct an optimal estimator for any given assumption as to the potential. The estimators do not explicitly depend on any properties of the density of the test objects, which is desirable as in practice such information is dominated by selection effects. As particular examples, we also develop estimators tailored for the problem of estimating the mass of the Hernquist or Navarro–Frenk–White dark matter haloes from the projected positions and velocities of stars.