Praise be to Allah.
There is no contradiction between being created to worship Allah and being created in order to be tested.
Worship in itself is a test from Allah, may He be exalted, to His slaves, so that He may make known who will believe and who will disbelieve, who will be disobedient and who will be obedient, then He will reward the doer of good for his good deeds and will punish the doer of evil for his evil deeds. Moreover, the reason for testing people is so as to see a person’s reaction at the time of calamity: Will he bear it with patience or will he panic? And it is to see his reaction when he is tested with blessings and calamities: will he give thanks or will he be grateful?
Perhaps because the questioner thought that there was a contradiction between the two matters, he thought that tests are limited to calamities, so whoever bears them with patience will attain reward, and whoever panics and shows ingratitude will incur a burden of sin and punishment.
This is indicative of a limited understanding of what is meant by tests and trials.
The correct understanding is that what is meant by tests and trials here is broader in meaning than calamity. Tests and trials include all the affairs and deeds of the son of Adam, and all the details of his life, for his life is a test, his good health is a test, his poor health is a test, his happiness is a test, his wealth is a test, his provision is a test, the environment in which he lives is a test, and his knowledge is a test. In all of that, Allah, may He be exalted, is testing this man and his behaviour to see whether he will follow the path of righteousness or the path of evildoing, whether he will obey the Most Gracious or will obey the Shaytaan. Hence Allah, may He be glorified and exalted, says (interpretation of the meaning):
“[He] who created death and life to test you [as to] which of you is best in deed - and He is the Exalted in Might, the Forgiving”
“And it is He who created the heavens and the earth in six days - and His Throne had been upon water - that He might test you as to which of you is best in deed”
“Had Allah willed, He would have made you one nation [united in religion], but [He intended] to test you in what He has given you; so race to [all that is] good. To Allah is your return all together, and He will [then] inform you concerning that over which you used to differ”
“And it is He who has made you successors upon the earth and has raised some of you above others in degrees [of rank] that He may try you through what He has given you. Indeed, your Lord is swift in penalty; but indeed, He is Forgiving and Merciful”
All these verses indicate that this test or trial is the reason for man’s creation; this test also includes the religious duties. So whoever carries out his duties towards his Lord – in all aspects of his life – will succeed, and whoever falls short will incur loss commensurate with his shortcomings.
Ibn Qayyim al-Jawziyyah (may Allah have mercy on him) said:
Allah, may He be exalted, has told us that the creation of the universe, death, life, and the making attractive of the earth and all that is on it are all for the purpose of test and trial, in order to test people as to which of them is best in deed, so that man’s deeds might be in accordance with what is pleasing to the Lord, may He be exalted, and thus will be in harmony with the purpose for which he was created and for which the universe was created, which is true servitude to Allah, which means loving Him and obeying Him. This is the best of deeds (as referred to in the verse quoted above), and such are the deeds that He loves and is pleased with.
End quote from Rawdat al-Muhibbeen (61).
Al-‘Allaamah Muhammad al-Ameen ash-Shinqeeti (may Allah have mercy on him) said, in his commentary on the verse (interpretation of the meaning), “And I did not create the jinn and mankind except to worship Me” [adh-Dhaariyaat 51:56]:
What this verse means, “except to worship Me” is: so that I may enjoin on them that they should worship Me and I will test them; that is, I will test them with religious duties, then I will requite them for their deeds: if their deeds are good, then the outcome will be good, and if their deeds are evil, then the outcome will be bad.
The only reason why we say that this is what the verse means is because this is indicated by many clear verses in the Book of Allah. He, may He be exalted, clearly stated in several verses of His Book that He created them in order to test them as to which of them is best in deed, and that He will requite them for their deeds.
Allah, may He be exalted, says at the beginning of Soorat al-Kahf (interpretation of the meaning):
“Indeed, We have made that which is on the earth adornment for it that We may test them [as to] which of them is best in deed”
These verses clearly state that the wisdom behind His creation of mankind is to test them as to which of them is best in deed, and this explains the word “except to worship Me” [adh-Dhaariyaat 51:56], because the best thing to explain the Qur’an is the Qur’an.
It is known that the outcome of the deeds referred to can only be achieved by rewarding the doer of good for his good deeds and requiting the doer of evil for his evil deeds. Hence Allah, may He be exalted, clearly stated that the reason for creating them, first of all, then resurrecting them later on, is to reward the doer of good for his good deeds and to requite the doer of evil for his evil deeds. This is seen in the verse at the beginning of Soorat Yoonus in which Allah, may He be exalted, says (interpretation of the meaning):
“Indeed, He begins the [process of] creation and then repeats it that He may reward those who have believed and done righteous deeds, in justice. But those who disbelieved will have a drink of scalding water and a painful punishment for what they used to deny”
[Yoonus 10: 4].
And He says at the beginning of Soorat an-Najm (interpretation of the meaning):
“And to Allah belongs whatever is in the heavens and whatever is in the earth - that He may recompense those who do evil with [the penalty of] what they have done and recompense those who do good with the best [reward]”
Allah, may He be exalted, denounced man for thinking that he would be left neglected, that is, without purpose, with no commands or prohibitions, and He stated that He did not cause man to pass through the stages of development (in the womb) until he became a human being, except to resurrect him after death and then to requite him for his deeds, as He, may He be exalted, says (interpretation of the meaning):
“Does man think that he will be left neglected?
Had he not been a sperm from semen emitted?
Then he was a clinging clot, and Allah] created [his form] and proportioned [him].
And made of him two mates, the male and the female.
Is not that [Creator] Able to give life to the dead”
End quote from Adwaa’ al-Bayaan fi Eedaah al-Qur’an bi’l-Qur’an, 7/445.
And Allah knows best.