How do you know if your problems and failures in life are tests or punishment? can you tell difference between tests and punishment in life?
The short ansewer is that you cannot know this. Only Allah, or a person inspired by Allah i.e. a prophet knows this. But your are making the correct assumption that Allah both test us and he also punishes the wrong-doers in the worldly life.
Example of verses where Allah tells us that He is testing us are:
And verily We shall try you till We know those of you who strive hard (for the cause of Allah) and the steadfast, and till We test your record. (47:31)
This verse suggest that Allah will test people to their uttermost,
If ye have received a blow, the (disbelieving) people have received a blow the like thereof. These are (only) the vicissitudes which We cause to follow one another for mankind, to the end that Allah may know those who believe and may choose witnesses from among you; and Allah loveth not wrong-doers. (3:140) And that Allah may prove those who believe, and may blight the disbelievers. (3:141) Or deemed ye that ye would enter paradise while yet Allah knoweth not those of you who really strive, nor knoweth those (of you) who are steadfast? (3:142)
Many people are under the false impression that paradise is easy, but this is expressed no-where in the Quran.
And was it so, when a disaster smote you, though ye had smitten (them with a disaster) twice (as great), that ye said: How is this? Say (unto them, O Muhammad): It is from yourselves. Lo! Allah is Able to do all things. (3:165) That which befell you, on the day when the two armies met, was by permission of Allah; that He might know the true believers; (3:166) And that He might know the hypocrites, unto whom it was said: Come, fight in the way of Allah, or defend yourselves. They answered: If we knew aught of fighting we would follow you. On that day they were nearer disbelief than faith. They utter with their mouths a thing which is not in their hearts. Allah is best Aware of what they hide. (3:167)
Assuredly ye will be tried in your property and in your persons, and ye will hear much wrong from those who were given the Scripture before you, and from the idolaters. But if ye persevere and ward off (evil), then that is of the steadfast heart of things. (3:186)
See they not that they are tested once or twice in every year? Still they turn not in repentance, neither pay they heed. (9:126)
Yes people are tried and tested but one important thing to note is that this is only to what we can bear. Allah will not lay on a burden greater than we can bear.
Now Allah also punishes people for there wrongdoing on earth. They can be various reasons for this, see following verses:
(9:48) Of them is he who saith: Grant me leave (to stay at home) and tempt me not. Surely it is into temptation that they (thus) have fallen. Lo! hell verily is all around the disbelievers. (49) If good befalleth thee (O Muhammad) it afflicteth them, and if calamity befalleth thee, they say: We took precaution, and they turn away well pleased. (9:50) Say: Naught befalleth us save that which Allah hath decreed for us. He is our Protecting Friend. In Allah let believers put their trust! (9:51) Say: Can ye await for us aught save one of two good things (death or victory in Allah's way)? while we await for you that Allah will afflict you with a doom from Him or at our hands. Await then! Lo! We are awaiting with you. (9:52) Say: Pay (your contribution), willingly or unwillingly, it will not be accepted from you. Lo! ye were ever froward folk. (9:53) And naught preventeth that their contributions should be accepted from them save that they have disbelieved in Allah and in His messenger, and they come not to worship save as idlers, and pay not (their contribution) save reluctantly. (9:54) So let not their riches nor their children astonish thee (O Muhammad). Allah thereby intendeth but to punish them in the life of the world and that their souls shall pass away while they are disbelievers. (9:55)
These people were punished because they disbelieve in Allah and his messenger (pbuh).
Lo! those who love that slander should be spread concerning those who believe, theirs will be a painful punishment in the world and the Hereafter. Allah knoweth. Ye know not. (24:19)
The above is a quite interesting one, most people do not realise that the people who love to spread slander of believes are also punished in the world.
Make not the calling of the messenger among you as your calling one of another. Allah knoweth those of you who steal away, hiding themselves. And let those who conspire to evade orders beware lest grief or painful punishment befall them. (24:63) Lo! verily unto Allah belongeth whatsoever is in the heavens and the earth. He knoweth your condition. And (He knoweth) the Day when they are returned unto Him so that He may inform them of what they did. Allah is Knower of all things. (24:64)
The above verses are for those who make invalid excuses when they are summoned by the messenger for example for Jihad.
Leave Me (to deal) with him whom I created lonely, (74:11) And then bestowed upon him ample means, (74:12) And sons abiding in his presence (74:13) And made (life) smooth for him. (74:14) Yet he desireth that I should give more. (15) Nay! For lo! he hath been stubborn to Our revelations. (16) On him I shall impose a fearful doom. (74:17) For lo! he did consider; then he planned - (74:18) (Self-)destroyed is he, how he planned! (74:19) Again (self-)destroyed is he, how he planned! - (74:20) Then looked he, (74:21) Then frowned he and showed displeasure. (74:22) Then turned he away in pride (74:23) And said: This is naught else than magic from of old; (74:24)
Allah also punishes people that reject the verses of the Quran.
This is not at all an exhorted list, but as you can see their are a variety of reasons Allah punishment descends on a person
One point to note is that, for the wrong-doer, this is only part of their punishment, they will also be punished in the hereafter.
Whether We shall show thee (within thy life-time) part of what we promised them or take to ourselves thy soul (before it is all accomplished),- thy duty is to make (the Message) reach them: it is our part to call them to account. (13:40)
To conclude Allah test and punishes us but ordinary people cannot make the Judgement that this person is being punished/tested only Allah and the messengers of Allah can know this. And they only reason the Messenger Know this is because Allah somehow tells them this. You will find this in the Quran and other scriptures:
in the Quran:
And when they appeal unto Allah and His messenger to judge between them, lo! a faction of them are averse; (24:48) But if right had been with them they would have come unto him willingly. (24:49) Is there in their hearts a disease, or have they doubts, or fear they lest Allah and His messenger should wrong them in judgment? Nay, but such are evil-doers. (24:50) The saying of (all true) believers when they are called unto Allah and His messenger to judge between them is only that they say: We hear and we obey. And such are the successful. (24:51)
this clearly shows that the messenger (pbuh) can judge people.
And in the Gospel Jesus (pbuh) says:
John 5:30, By myself I can do nothing; I judge only as I hear, and my judgment is just, for I seek not to please myself but him who sent me.