Tafsir Ibn Kathir -> Surah At-Taubah -> Among the Wisdom of Jihad is to test the Muslims

Allah said,

﴿أَمْ حَسِبْتُمْ﴾

(Do you think), O believers that We will leave you untested with matters that make apparent those who have pure, good intent from those who have false intent This is why Allah said next,

﴿وَلَمَّا يَعْلَمِ اللَّهُ الَّذِينَ جَـهَدُواْ مِنكُمْ وَلَمْ يَتَّخِذُواْ مِن دُونِ اللَّهِ وَلاَ رَسُولِهِ وَلاَ الْمُؤْمِنِينَ وَلِيجَةً﴾

(while Allah has not yet tested those among you who have striven hard and fought and have not taken Walijah besides Allah and His Messenger, and the believers...), meaning, supporters and confidants. Rather, they are sincere for Allah and His Messenger inwardly and outwardly. Allah also said;

﴿الم - ذَلِكَ الْكِتَابُ لاَ رَيْبَ فِيهِ هُدًى لِّلْمُتَّقِينَ - الَّذِينَ يُؤْمِنُونَ بِالْغَيْبِ وَيُقِيمُونَ الصَّلوةَ وَمِمَّا رَزَقْنَـهُمْ يُنفِقُونَ ﴾

(Alif-Lam-Mim. Do people think that they will be left alone because they say: "We believe,'' and will not be tested. And We indeed tested those who were before them. And Allah will certainly make known those who are true, and will certainly make known those who are liars...) ﴿29:1-3﴾,

﴿أَمْ حَسِبْتُمْ أَن تَدْخُلُواْ الْجَنَّةَ وَلَمَّا يَعْلَمِ اللَّهُ الَّذِينَ جَـهَدُواْ مِنكُمْ وَيَعْلَمَ الصَّـبِرِينَ ﴾

(Do you think that you will enter Paradise before Allah tests those of you who fought (in His cause) and (also) tests those who are patient)﴿3: 142﴾, and,

﴿مَّا كَانَ اللَّهُ لِيَذَرَ الْمُؤْمِنِينَ عَلَى مَآ أَنتُمْ عَلَيْهِ حَتَّى يَمِيزَ الْخَبِيثَ مِنَ الطَّيِّبِ﴾

(Allah will not leave the believers in the state in which you are now, until He distinguishes the wicked from the good)﴿3:179﴾. In summary, since Allah legislated Jihad for His servants, He explained that the wisdom behind doing so includes testing His servants, distinguishing between those who obey Him and those who disobey Him. Allah, the Exalted, is the All-Knower of what occurred, what will occur, and the true essence of what might occur had He decided it. Therefore, Allah knows everything before it occurs and how it will occur, there is no deity worthy of worship except Him, nor a Lord except Him. Truly, there is none who can avert Allah's judgment and decision.

﴿مَا كَانَ لِلْمُشْرِكِينَ أَن يَعْمُرُواْ مَسَاجِدَ الله شَـهِدِينَ عَلَى أَنفُسِهِم بِالْكُفْرِ أُوْلَـئِكَ حَبِطَتْ أَعْمَـلُهُمْ وَفِى النَّارِ هُمْ خَـلِدُونَ - إِنَّمَا يَعْمُرُ مَسَـجِدَ اللَّهِ مَنْ ءَامَنَ بِاللَّهِ وَالْيَوْمِ الاٌّخِرِ وَأَقَامَ الصَّلَوةَ وَءاتَى الزَّكَوةَ وَلَمْ يَخْشَ إِلاَّ اللَّهَ فَعَسَى أُوْلَـئِكَ أَن يَكُونُواْ مِنَ الْمُهْتَدِينَ ﴾

(17. It is not for the Mushrikin, to maintain the Masjids of Allah, while they witness against themselves of disbelief. The works of such are in vain and in Fire shall they abide.) (18. The Masjids of Allah shall be maintained only by those who believe in Allah and the Last Day; perform the Salah, and give Zakah and fear none but Allah. It is they who are on true guidance.)