Self-testing refers to the fact that, in some quantum devices, both states and measurements can be assessed in a black-box scenario, on the sole basis of the observed statistics, i.e., without reference to any prior device calibration. Only a few examples of self-testing are known, and they just provide nontrivial assessment for devices performing unrealistically close to the ideal case. We overcome these difficulties by approaching self-testing with the semidefinite programing hierarchy for the characterization of quantum correlations. This allows us to improve dramatically the robustness of previous self-testing schemes; e.g., we show that a Clauser-Horne-Shimony-Holt violation larger than 2.57 certifies a singlet fidelity of more than 70%. In addition, the versatility of the tool brings about self-testing of hitherto impossible cases, such as the robust self-testing of nonmaximally entangled two-qutrit states in the Collins-Gisin-Linden-Massar-Popescu scenario.