Solenoid field errors have great influence on electron beam qualities. In this paper, design and testing of high precision solenoids for a compact electron linac is presented. We proposed an efficient and practical method to solve the peak field of the solenoid for relativistic electron beams based on the reduced envelope equation. Beam dynamics involving space charge force were performed to predict the focusing effects. Detailed optimization methods were introduced to achieve an ultra-compact configuration as well as high accuracy, with the help of the POISSON and OPERA packages. Efforts were attempted to restrain system errors in the off-line testing, which showed the short lens and the main solenoid produced a peak field of 0.13T and 0.21T respectively. Data analysis involving central and off axes was carried out and demonstrated that the testing results fitted well with the design.