So if Allah already knows what we are going to do, why does he still try us?

Allah has set in place a system of reward and punishment. If He did not try us as then we would have an excuse on the day of judgement and say why did you choose hell for me when you never gave me a test or opportunity to achieve paradise? This would be against Allahs justice. He gives us the opportunity by testing us to do right or wrong.

I remember a fellow scholar speaking about this same question and will also send you his reply

https://youtu.be/vtU5_BSfu_w