A general framework for unbiased tests of gravity using galaxy clusters