The object of this paper is the development of a theory of optimal one-sample goodness-of-fit tests and of optimal two-sample randomized distribution-free (DF) statistics analogous to the well-known results of Hoeffding (1951), Terry (1952), Lehmann (1953), (1959), Chernoff and Savage (1958), Capon (1961) and others for two-sample nonrandomized rank statistics. For $Y_1, \cdots, Y_n$ a random sample from a population with continuous distribution function $G$, one tests in the one-sample case $H_0 : G = F$ vs. $H_1 : G \neq F$, where $F$ is some known continuous distribution function. From the Neyman-Pearson lemma, distribution-free tests that are most powerful (MP) for any $H$ vs. $K$ satisfying $KH^{-1} = GF^{-1}$, are obtained. From these MP distribution-free tests, one can on paralleling the derivations ([14], [25], [17], [18], [7]) for locally MP tests in the two-sample case obtain locally MP tests in the one-sample case. Further, it is found that the class of alternatives, for which a critical region of the form $\lbrack\sum J\lbrack F(y_i)\rbrack > c\rbrack$ is locally MP, is the class of $G$'s that consists of "contaminated" Koopman-Pitman distributions as given in Section 5. Randomized versions of the two-sample MP and locally MP rank statistics are considered and shown to be asymptotically equivalent to the locally MP rank statistics.