Suppose we are given a dataset with $T$ time periods and $N$ assets or portfolios. We are interested in estimating and testing the CAPM. Using Fama-MacBeth style analysis, we first estimate $N$ time series regressions of the form $$ r^*_{i,t}=\alpha_i+\beta_i r^*_{m,t}+u_{i,t} \tag{1} $$ for $i=1,\dots,N$ where $r^*_i:=(r_{i,t}-r_{f,t})$ is asset's $i$ excess return and $r^*_m:=(r_{m,t}-r_{f,t})$ is the market's excess return, $r_{f,t}$ being the risk-free rate. This way we obtain the fitted betas $\hat\beta_i$ for $i=1,\dots,N$. We then estimate $T$ cross-sectional regressions $$ r^*_{i,t}=\alpha_i+\lambda_t\hat\beta_i+v_{i,t} \tag{2} $$ for $t=1,\dots,T$.
We then obtain $\hat{\lambda} = \frac{1}{T} \sum^{T}_{t=1} \hat{\lambda}_t$ and its estimated asymptotic variance as $\widehat{\text{AVar}}(\hat{\lambda}) = \frac{1}{T^2} \sum^{T}_{t=1} (\hat{\lambda}_t - \hat{\lambda} )(\hat{\lambda}_t - \hat{\lambda} )'$. However, we could skip this bit if we are not directly interested in inference about the true $\lambda$.
The CAPM implies $\alpha_i=0 \ \forall i$, and an assessment of $H_0\colon \alpha_i=0 \ \forall i$ is a common test of the CAPM.
Update: The following is based on a mistaken thought about what the CAPM implies. I realized that with the help of Matthew Gunn. I leave it as is for historical consistency.
If only $(2)$ contained $\beta_i$s in place of $\hat\beta_i$s, the CAPM would also imply $\lambda_t=r^*_{m,t}$. Thus, I would be tempted to impose $\lambda_t=r^*_{m,t}$ in $(2)$ to yield $$ r^*_{i,t}=\alpha_i+\hat\beta_i r^*_{m,t}+w_{i,t} \tag{3}. $$ Since the systematic risk component $\hat\beta_i r^*_{m,t}$ is given, what would have to be estimated would be the $\alpha_i$ in $$ (r^*_{i,t}-\hat\beta_i r^*_{m,t})=\alpha_i+w_{i,t} \tag{3'}, $$ allowing us to test $H_0\colon \alpha_i=0 \ \forall i$ later on. The intuition behind wanting to impose $\lambda_t=r^*_{m,t}$ is that estimating something that we actually observe sounds like a way to introduce unnecessary noise in the model.
Now, I do see the subtle difference (which may be not so subtle numerically) between $\hat\beta_i$ and $\beta_i$ that does not allow us to impose $\lambda_t=r^*_{m,t}$. (That would make $\alpha_i$ in $(3)$ include the $\alpha_i$ from $(1)$ plus $(\beta_i-\hat\beta_i) r^*_{m,t}$.) Is this the reason for why we estimate $\lambda_t$ in $(2)$? Or is there some other reason for (roughly speaking) estimating the market return when we actually observe it?