Together, stomach and oesophageal cancer account for around 1.4 million new cancer diagnoses each year worldwide, researchers said.
Both tend to be diagnosed late, because the symptoms are ambiguous, meaning the five-year survival rate for these two types of cancer is only 15 per cent.
The new research, involving more than 300 patients, showed that the test could diagnose cancer with an overall accuracy of 85 per cent.
"At present the only way to diagnose oesophageal cancer or stomach cancer is with endoscopy. This method is expensive, invasive and has some risk of complications," said Sheraz Markar from Imperial College London in the UK.
More From This Section
The trial was based on the results of previous research that suggested differences in the levels of specific chemicals (butyric, pentanoic and hexanoic acids, butanal, and decanal) between patients with stomach or oesophageal cancer and patients with upper gastrointestinal symptoms without cancer.
The new research aimed to test whether this 'chemical signature' that seemed to typify cancer could be the basis of a diagnostic test.
All the samples were analysed with a technique called selected ion flow-tube mass spectrometry, which is able to accurately measure small amounts of different chemicals in mixtures of gases such as breath.
Researchers measured the levels of the five chemicals in each sample to see which ones matched to the 'chemical signature' that indicated cancer.
The results showed that the test was 85 per cent accurate overall, with a sensitivity of 80 per cent and a specificity of 81 per cent.
Over the next three years, the researchers will continue with a larger trial, using the test with patients who are being given an endoscopy for gastrointestinal symptoms but not yet diagnosed with cancer.
The team is also working on breath tests for other types of cancer, such as colorectal and pancreatic, which could be used as first-line tests in general practice surgeries.