Sounds like you'd want to do a self-join. Something like this would utilize indexes (assuming you have an index on user_id and an index on test_id, which you should):
SELECT DISTINCT t1.*
FROM `tableName` t1
INNER JOIN `tableName` t2 ON t2.user_id = t1.user_id
WHERE t2.test_id = 5
I guess that raises another point. How is your table indexed? Just the numeric primary key? You generally want to have indexes on all columns that are used in JOINs or WHERE clauses. Thus, you'd want to have an index on user_id and an index on test_id.
Can a user take the same test multiple times? If not, then you'd want restrict that by having a unique multiple-column ("composite") index across user_id and test_id together. And then add a regular index just to test_id for the WHERE clause.