The answer for Allah is simple and straight forward: "it is a test"
The answer for evolution is more complex.
It can be any of or a combination of the following answers (and there can possibly be more):
It is possible that homosexuals actually do reproduce more because of their homosexuality. Homosexuality is seldom exclusive. So, homosexuals in nature could still have sex with the opposite sex and maybe their homosexuality would make them more successful in finding a reproduction mate or would make them more successful in raising their offspring.
It is possible that homosexuals help their close relatives reproduce. This would help the genes that promote homosexuality. An example is ants: Worker ants are sterile, yet they make sure the ant Queen can reproduce. Maybe, genes that promote homosexuality help their own reproduction by creating family members that care for their siblings, nieces and nephews.
It is possible that genes that promote homosexuality are beneficial because they also enhance the chance of survival of some of their carriers (Genes often have more than one effect)
It is possible that genes that promote homosexuality are beneficial because they make some of their carriers more fertile (Genes often have more than one effect). E.g. it could be possible that one of the genes that promote homosexuality in males promotes fertility in females.
It is possible that genes promote homosexuality in some environment but promote something else in another environment.
It is possible that genes that promote homosexuality lead to homosexuality in the presence of some genes and beneficial in the presence of some other genes.
It is possible that genes that promote homosexuality are close to other genes that are beneficial and are therefore most often passed on together with those beneficial genes.
...
I don't think it's genetic. This study shows that it could be a result of the womb environment-
http://news.bbc.co.uk/1/hi/health/5120004.stmNewScientist has an interesting study on the brains of gays and straights-
http://www.newscientist.com/article/dn14146-gay-brains-structured-like-those-of-the-opposite-sex.htmlAt the end of the article it mentions this-
"But as Savic herself acknowledges, the study can't say whether the brain differences are inherited, or result from abnormally high or low exposure
in the womb to sex hormones such as testosterone."
So there are two possibilities of what may cause homosexuality.