asalam alaikum,
Life in general is a test.. and Allah says in the Holy Qur'an that He will test us in various ways.
in Surah Kahf where Khidr (RA) had been ordered by Allah to kill a young boy, Musa objected, and was ignorant of the wisdom of Allah subhana wa ta'ala behind the killing, just as we are ignorant about why things happen that we do not understand..
So why did Allah subhana wa ta'ala tell Khidr to kill the boy? The answer is in verses 80/81 when Khidr explains to Musa the meaning of which he (Musa) had no patience for...
Q:18:80: "As for the lad, his parents were believers and we feared lest he should oppress them by rebellion and disbelief."
(18:81) "And we intended that their Lord should change him for them for one better in purity and nearer to mercy."
Can you see the reason? It is our belief that Allah is the All-Knower and we trust His judgment in His will. Things happen that may not make sense to us, but as only Allah knows that thing may be the best for us in the long run. Whether Allah decides to take a baby's life to test the mother, or to save the mother from a would-be tyrant, it is all by Allah's will. And that's that, because Allah knows best.