Alkaline earth metals except Be and Mg impart a characteristic colour to the Bunsen burner flame in the flame test.
The different colours arise due to the different energies (provided by heat in this case) for the states of electron excitation and de-excitation.
The valence electrons in Be and Mg are tightly bound to the atom due to the small atomic and ionic size.
Be and Mg has very high ionization energy in comparison to other alkaline earth metals due to their small size.
The energy of the Bunsen flame is not sufficiently high in order to excite the electrons of Be and Mg.
Hence Be and Mg do not show any colour in flame test.