The main result of this paper is a near-optimal derandomization of the affine homomorphism test of Blum, Luby and Rubinfeld (Journal of Computer and System Sciences, 1993). We show that for any groups G and , and any expanding generating set S of G, the natural deramdomized version of the BLR test in which we pick an element x randomly from G and y randomly from S and test whether f(x)?f(y) = f(x?y), performs nearly as well (depending of course on the expansion) as the original test. Moreover we show that the underlying homomorphism can be found by the natural local "belief propagation decoding". We note that the original BLR test uses 2 log2 |G| random bits, whereas the derandomized test uses only (1 + o(1)) log2 |G| random bits. This factor of 2 savings in the randomness complexity translates to a near quadratic savings in the length of the tables in the related locally testable codes (and possibly probabilistically checkable proofs which may use them). Our result is a sig...