The invention discloses an electric control compression testing machine and testing method for in-situ imaging by using high-energy X-rays. A high-precision servo motor is adopted for actuation; the rotary motion of the motor is converted into the up-and-down linear motion of the lower clamp by utilizing a two-stage worm gear reducer; a sample is fixed through the upper clamp and the lower clamp,compressive stress is applied to the sample through displacement control of the lower clamp, loads and displacement borne by the sample in the testing process are collected in real time through the miniature dynamic force sensor and the laser vibrometer, and closed-loop control over the testing machine is achieved through the control unit. The testing machine disclosed by the invention is an in-situ compression material testing device which has the characteristics of high precision, large load, small size, light weight, capability of realizing monotonous compression and the like; good compatibility with a synchrotron radiation light source test platform can be realized, the requirements of a sample platform on weight and size are met, and the internal structure and typical defects of a material under various monotonous loading stress levels can be monitored in real time.