You should be using sklearn.utils.testing.assert_warns_message here: getting this code fully right is tricky because of side-effects created in the warning registry.