How will we know whether we are being tested by Allah swt or being punished for sins

If it brings you closer to God then it is a test, and if it takes you further away then it is a punishment. (Paraphrased from Hadith).