The Attitudes of the Hypocrites and the Ways in which Allah tests People
Allah mentions the descriptions of the liars who falsely claim faith with their lips, while faith is not firm in their hearts. When a test or trial comes in this world, they think that this is a punishment from Allah, so they leave Islam. Allah says:
(Of mankind are some who say: "We believe in Allah." But if they are made to suffer for Allah, they consider the trial of mankind as Allah's punishment;) Ibn `Abbas said, "Meaning that their trial is leaving Islam if they are made to suffer for Allah." This was also the view of others among the Salaf. This Ayah is like the Ayah,
(1)-It means that such people join the infidels when they apprehend some kind of torture from them, but when Allah's help will come to the Muslims in a battle against the infidels, they will pretend to be Muslims on the plea that they had joined the infidels only because they feared to be persecuted by them.
(2)-The sense is that they did not believe in Islam with their hearts, and this fact cannot: be concealed from Him, because He knows whatever lies in the hearts of the people.
Tazkirul Quran
Maulana Wahiduddin KhanOn ayahs 11–12
When one who calls himself a believer, or mu’min, shows himself to be such only when some advantage is to be gained, but otherwise recants, he reveals himself to be what the Quran calls a hypocrite, or munafiq. A believer in name only, he is not prepared to pay the price of his faith. He and other people like him fail at the very juncture where they are most required to prove their moral worth.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.