It is permissible to masturbate for the purpose of medical testing
Yes, that is permissible. It says in Fataawa al-Shaykh ‘Abd al-‘Azeez ibn Humayd (may Allaah have mercy on him) that he was asked about that and he replied:
There is nothing wrong with that so long as there is a need for it. Whoever masturbates without there being a need for it should be punished. But this is for a need, which is to produce semen for testing and to find out the reason why this person is not having children. The reason for that may rest with him or with his wife, so in this case there is nothing wrong with it in sha Allah.
From Fataawa al-Shaykh ‘Abd al-‘Azeez ibn Humayd, p. 271.
And Allaah knows best.
Don't miss out to watch Islamhelpline sponsored Islamic Kids Competition