You may be thinking of "The Test", a novelette by Stanisław Lem in his Pirx the Pilot series.
[![enter image description here]]
It appeared in Omni, October 1979, and is available online from William Flew.
The story is about an astronaut launched into space on a training exercise, but he doesn't know it is only a "virtual" trip.
"Cadet Pirx, ready for lift-off."
He gets up into space in his small ship, and as he is communicating back to mission control, he becomes aware of a tiny hitchhiker: a fly is along with him, inside his helmet.
Not inside his helmet, but in the cabin:
It was a giant of a fly, one of those ugly, greenish-black brutes specially designed to make life miserable—a pestering, pesky, idiotic, and by the same token shrewd and cunning fly, which had miraculously—and how else?—stowed away in the ship's control cabin and was now zooming about in the space outside the blister, occasionally ricocheting off the illuminated instrument gauges like a buzzing pellet.
And it's not one but two flies:
For a moment the fly seemed to multiply, to be in two different places at once. Pirx rubbed his eyes. Just as he suspected: there was not one, but two of them. Where did the second one come from?!
He doesn't freak out but attends to it as if it were a fellow voyager up from Earth, and only a minor inconvenience.
Pirx does not develop any friendly feeling for the flies:
There was another buzzing noise, this time very faint. The other fly! It was alive, the bastard! Alive and buzzing the blister's ceiling.
When he completes the exercise and comes out of his space ship, he meets his teachers who tell him he passed the "fly test".
No mention of the flies, but he passed the test:
The moment the glass bubble went up, Pirx automatically started undoing his straps, then rose to his feet. The video screens in back of the CO went blank.
"A good performance, Pirx," said the CO. "Quite good."
A fellow astronaut who took the same test is being led away, to be treated for the extreme mental trauma caused by the same ordeal – but he had dealt with it differently. He freaked out.
They were opening the hatch of the other ship. The CO was standing on the catwalk, listening to something the men in white smocks were telling him.
A faint banging noise could be heard coming from the inside.
Then, from out of the cabin staggered a writhing hulk of a man in a brown uniform, his helmetless head bobbing around like a blurry blotch, his face contorted in a mute shriek. . . .
Pirx's knees buckled.
It was . . . Boerst.
He had crashed into the Moon.