And indeed Allah knows those who believe, and verily He knows the hypocrites.
Allah will test the people with calamities and with times of ease, so that He may distinguish the believers from the hypocrites, to see who will obey Allah both in times of hardship and of ease, and who will obey Him only when things are going in accordance with their desires.
As Allah says:
وَلَنَبْلُوَنَّكُمْ حَتَّى نَعْلَمَ الْمُجَـهِدِينَ مِنكُمْ وَالصَّـبِرِينَ وَنَبْلُوَ أَخْبَـرَكُمْ
And surely, We shall try you till We test those who strive hard and the patient, and We shall test your facts. (47:31)
After the battle of Uhud, with its trials and tribulations for the Muslims, Allah said:
مَّا كَانَ اللَّهُ لِيَذَرَ الْمُوْمِنِينَ عَلَى مَأ أَنتُمْ عَلَيْهِ حَتَّى يَمِيزَ الْخَبِيثَ مِنَ الطَّيِّبِ
Allah will not leave the believers in the state in which you are now, until He distinguishes the wicked from the good... (3:179)