There are a number of different methods used to test for how much caffeine is in a particular beverage. The tests used depend on a number of factors, including how precise the measurements need to be. In a lab setting the three most common tests are liquid chromatography, gas chromatography, and UV spectroscopy.
Both liquid and gas chromatography operate on much the same principle, passing a mobile phase of something through a stationary bed to separate parts out. Basically, whatever you are trying to test is passed into a column of some sort of material. The various parts, such as the caffeine in a drink, separate out because of the different affinities they have for whatever is used as the stationary bed. The output of this process, a chromatogram, is a graph with different peaks, with each peak representing a different component. So once a test like this is run on a drink, whoever is doing the test just needs to look at the chromatogram for the caffeine peak, to determine how much caffeine was present in the overall mixture.
In liquid chromatography, whatever is being tested will remain in a liquid form while passing through the stationary phase. Liquid chromatography can take place in a column, but can also take place on a plane. For more precision and with higher-pressure, a liquid can be subjected to what is called high-performance liquid chromatography (HPLC), which is in fact what most very precise determinations of caffeine content use. In gas chromatography the liquid being tested is put in a gaseous state along with a mobile gas such as Helium before passing through the stationary phase.
In UV spectroscopy, a light is passed through a liquid, and the way the light comes out the other side helps scientists determine the caffeine content of the liquid. Various compounds have different wavelengths, and these are rather precise. By seeing how the color of the light that passes through the liquid changes, scientists can very specifically determine the amount of different compounds that are in the liquid. This is a fairly popular method of determining caffeine in liquids, and recent advances in the field have made it even more specific.
Recently, a new method of determining whether or not a drink has caffeine in it has surfaced. Using heat-resistant antibodies from llamas and camels, scientists have come up with a highly-portable device that can be dropped in a cup of coffee or tea or any other drink to detect the presence of caffeine. Although this technique doesn’t give exact amounts, it is useful for people who are trying to avoid caffeine entirely in their diets, and want to make sure the decaf they order is in fact non-caffeinated.