As Muslims we believe in destiny and that our fate has already been determined and our destiny has been written by God as he is all knowing.
So in essence, God knew what was going to happen with his creation before he created, he knew the good and the evil that was going to occur.
With that being said, why did God decide to create us? We cannot say it's a test because he already knew what will happen.
“No calamity befalls on the earth or in yourselves but it is inscribed in the Book of Decrees (Al‑Lawh Al‑Mahfooz) before We bring it into existence. Verily, that is easy for Allaah - [al-Hadeed 57:22]