Due to the small atomic size, Be and Mg have higher ionisation energy than other alkaline earth metals. So, the energy of flame is not sufficient to excite their electrons. As a result, they do not impart colour to the flame test.
Beryllium and magnesium do not impart color to the flame in a flame test because they do not produce strong enough spectral lines to be visible to the naked eye. In a flame test, the sample is heated and the electrons in the sample are excited to higher energy levels. As the electrons return to their ground state, they emit light in the form of spectral lines, which can be used to identify the element. However, the spectral lines produced by beryllium and magnesium are weak and not easily visible to the naked eye, resulting in no noticeable color change in the flame.