AbstractLet us consider the locus in the moduli space of curves of genus$2k$defined by curves with a pencil of degree$k$. Since the Brill–Noether number is equal to$- 2$, such a locus has codimension two. Using the method of test surfaces, we compute the class of its closure in the moduli space of stable curves.