Question: Allah says in the Holy Quran:
Now the question is that testing is required by one who is not aware of something and one who is not aware of the result. While for Allah apparent and hidden is all same and whatever it is in the Heavens and the earth; nothing is hidden from Him. Then why is it necessary for Him to test His servants?
Answer: We should keep in mind that the purpose of test and trial by Allah is something different. The man himself because of his deficiency and lack of wisdom and understanding is compelled go through the test to find out the truth, and to remove all kinds of ambiguity. However, such a situation is impossible for Allah. He is well aware of everything in the light of His unprecedented and unlimited knowledge, therefore in that sense, His taking a test is not akin to wisdom. Rather, the test is in some other sense and for some other purpose, the explanation of which is given below:
(1) This can be explained in a way that when a person is born, there are so many capabilities and astonishing potentialities in his nature. All the human perfections and ethical proficiencies are hidden in him in the form of talent. And his temperament is made from that. These capabilities are like yarns under the ground, which do not come out without proper means and do not reach upto the stage of power and excellence. This is a matter of commonsense that, as long as these capabilities are not coming into open, till the time, evolution and excellence and as a result of it, reward and blessing cannot have a visible existence. The purpose of such tests is the upbringing of humans and cause to grow up in him high quality of virtues. If these responsibilities and tests were not there in the existence of man there the capabilities would never have become apparent. It could never have been visualized and no one would have been eligible for reward.
This is mentioned by Amirul-Momineen (‘a) in a short but very meaningful way in a paragraph in Nahjul Balagha. He says:
“Never say that, O Allah! I seek refuge from tests and trials from You, because, there is no one who is not put to trials, rather, while doing prayers (or asking for favors) say this that, O Allah! I seek refuge from You from such trials which may cause me to go astray (means those trials, in which I may not come out successful and through it I may not be able to make myself perfect (accomplished)…
After that Imam Ali (‘a) explains:
The objective of the test and trials is not to gain information and insight, because in the universe there is not a thing which is hidden from or unseen by Allah. The intention is that those hidden qualities say like, happiness and pleasure and anger or the portion of rage and fury which is given by Allah to become manifest and these hidden qualities come to out in the form of action and apparent deeds, so that reward and punishment and merit of penalty and requital becomes manifest.
As you have observed, Imam (‘a) has pointed out the purpose of test that, by them qualities and capabilities of man take the shape of apparent action and after this the person becomes competent for reward and punishment otherwise only on the basis of inner qualities (without the external acts) no reward or punishment can be given. And as a matter of fact in this situation perfection will not be achieved.
For example when Allah commanded Hazrat Ibrahim (‘a) to sacrifice Hazrat Ismail (‘a). When Allah tested him, His purpose is not to know whether Hazrat Ibrahim (‘a) would obey His command or not, but, the intention is that, the soul which was present in the self of Hazrat Ibrahim (‘a) which was eager to obey and accept the commands of Allah, the test will make it perfect, it will make it realize and take it into the stage of action and in this way Ibrahim (‘a) will move on the road of perfection (Pay attention).
Hence, Allah puts everyone to test and trials by difficulties, hardship, distress and misfortunes; as He says:
Difficulties and hardships are just like a furnace, which makes the iron hard and strong. Man also becomes strong and powerful in difficulties and misfortunes. And he becomes capable of removing obstacles, which come in his way to a successful and happy life.
When we say that, by taking tests, the purpose of Allah is to develop the latent good qualities in His servants it does not mean that all the persons who have undergone tests would necessarily develop good and likable qualities. But the intention is that tests from Allah form the base in the social atmosphere for upbringing and nourishment. Those who wish good fortune take full advantage of such circumstances and acquire special upbringing in this way. But some people take undue advantage from it and their evil and contemptible hidden qualities come into open and take the shape of evil actions. In technical terms they fail in their tests.
One of the mysteries of trials and tests from Allah is:
2) People can be recognized as who are virtuous and who are evil (sinful), who are believers and who are hypocrites and who are good and who are bad. The Holy Quran indicates towards this point by the word “Tamhees” and says:
Allah separates the believers from unbelievers by tests and these two groups are recognized as different and opposite to each other.
3) The third benefit of tests from Allah is this that it is the fulfillment of a condition on the false faiths which on general conditions in normal circumstances make thousands of claims and when the time of action comes they are proved worthless. That is they are heroes in talks and not in character. By tests and trials they are exposed. They are shown in their true colors.
If these types of people are not put in the furnace of tests, and their hollow inside is not disclosed which is contrary to their adorned and decorated explicit, then it is possible that he himself also may remain under a wrong impression and others also are enthralled in the fallacy. And the punishment given by Allah or the deprivation from the favors of Allah may be considered unjust, but the tests tear the curtain from reality and the fact of their existence their real self becomes evident to themselves as well as to others.
This is the logic behind the tests from Allah
 Surah Mulk 67:2
 Surah Baqarah 2:155
 Surah Aale Imran 3:141