According to Legal Match, witnesses do not have to testify in court if they incriminate themselves by doing so or are involved in a privileged relationship that is recognized by the courts. In all other cases, witnesses must testify when called upon or be subject to legal consequences.
There are a few types of relationships that can void the requirement to testify. A priest, for example, cannot testify to anything told to him during confession or counseling, and attorneys or doctors cannot be forced to testify against a client. Spouses also cannot be forced to testify against each other.