I kind of know what you are saying, I secertly do the same thing with people, I will test them to prove their loyalty or to see if they like me as much as I think I should be adored. The mind is a funny thing, it can play more tricks than a David Copperfield performance. Sadly, in most cases where I have had a long term friendship or something else go bad I do not really care about losing that person. What I do regret is that I do not have many close relationships with people of substance. Only you can truly know if a relationship is worth keeping don't maintain one becasue you are afraid of being alone or for the convience factor.
I think we all fall into a circular pattern with relationships at times. It's a cliche, but it's good for a start that you are aware that you are doing this. I see so many people asking why things turn out the way they do when they need to study their own behaviour, no one will turn around and 'save' them. But yes, sorry, I rambled...
You do it because theres a part of you somewhere that doesn't really feel like you deserve something good on that level, and you can't respect anyone who sees something like that that is worthwhile in you when you don't.
Go for the roots when you feel you've had enough. It's all there, and you likely deserve to be happy in the ways you just don't understand.