There are 2 reasons found:
1. If the system has no krb5.conf file, tests fail. This can be fixed by always accessing krb5.conf related methods after a krb5.conf is created.
2. Due to different DNS configurations, "localhost" may be canonicalized to "localhost", "127.0.0.1", or even "localhost.localdomain". This can be fixed by first trying out the canonicalized form and use this form in the service principal name.