If Allah (awj) is omniscient, why does He need to test mankind?

As implied by the question, because Allah (awj) is omniscient, the purpose of His tests is not to unveil something hitherto unknown. Rather, the Qur’an, and ahadith suggest that these tests serve two purposes. First, they comprise a Divine precedent (sunnah ilahiyyah [1]) that is in turn based on another Divine precedent that can be termed “universal guidance.” In particular, Allah (awj) wishes that mankind, by obeying the divine law (shari’ah) and struggling through the various circumstances that he encounters in life, will blossom from potentiality to actuality, and will thereby attain the level of perfection that has been intended for him. One of the terms that is used for such tests in Islamic literature is fitnah (in this sense this term denotes a “crucible”). Fitnah literally refers to the process of smelting by which gold is purified. In one hadith it is said that people are tested the way gold is tested. The mettle of man is gold ore that is purified in the crucible of Divine tests and guidance. It should be kept in mind though, that in the course of such tests, just as good qualities reach their perfection in the believers, bad qualities also develop to the utmost in disbelievers.

The second purpose that these tests serve is to awaken man from his sleep of heedlessness. The Qur’an, explains that one of the purposes of tragedy and disaster is to test man and thereby jar him from his stupor of heedlessness. In a way, tragedy in man’s life serves the same purpose as the ridges that are embossed on some modern motorways and highways that are meant to wake drivers during the monotony of highway driving so they do not fall asleep at the wheel.

There are, in reality, two types of Divine will at play here. One is called the existential will (al-iradah al-takwiniyyah) and the other is called the legislative will (al-iradah al-tashri’iyyah). Allah’s (awj) existential will is that through Divine tests, both believers and disbelievers be able to actualize their good and bad potentials. On the other hand, his legislative will is that only good potentials be actualized.