Giving testimony is a communal obligation, and may be an individual obligation in two cases: when a person is summoned to give testimony or when people’s rights will be affected if he does not give testimony.
Please see the answer to question no. 147934.
Allah, may He be exalted, has enjoined establishing justice and giving testimony on the basis of fairness, in all situations and in the interests of all people.
Allah, may He be exalted, says (interpretation of the meaning):
“O you who believe! Stand out firmly for justice, as witnesses to Allah, even though it be against yourselves, or your parents, or your kin”
Ibn al-Qayyim (may Allah have mercy on him) said:
In this verse, Allah, may He be glorified, commands us to stand out firmly for justice, which is equity. This is a command to be just towards everyone, whether he is an enemy or a friend. End quote from ar-Risaalah at-Tabookiyyah, p. 31
And Allah, may He be exalted, says (interpretation of the meaning):
“O you who believe! Stand out firmly for Allah and be just witnesses and let not the enmity and hatred of others make you avoid justice. Be just: that is nearer to piety”
As-Sa‘di (may Allah have mercy on him) said:
As you would give testimony in favor of your friend, then give testimony against him; as you would give testimony against your enemy, then give testimony in his favour, even if he is a disbeliever or innovator, for it is obligatory to be just towards him.
End quote from Tafseer as-Sa‘di, p. 224
It was narrated from Ibn ‘Umar: ‘Abdullah ibn Rawaahah used to go to the Jews of Khaybar every year to estimate their crops and take half of them (in accordance with the terms of an agreement between the Muslims and the Jews). They complained to the Messenger of Allah (blessings and peace of Allah be upon him) about his strictness, and they wanted to bribe him. He said: O enemies of Allah, are you offering me haraam earnings? By Allah, I have come to you from the dearest of people to me, and you are more hateful to me than apes and swine. But my hatred of you and my love of him does not make me treat you with justice.
They said: By virtue of this (principle) the heavens and earth are maintained.
Narrated by Ibn Hibbaan (5199) and others; classed as hasan by al-Albaani in at-Ta‘leeqaat al-Hassaan, 5176.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
All goodness is included in fairness and justice, and all evil is included in injustice. Hence justice is obligatory in all things and for all people, and injustice is forbidden in all things and to all people. It is not permissible to be unjust towards anyone at all, whether he is a Muslim or a disbeliever, or he is a wrongdoer…
End quote from Majmoo‘ al-Fataawa, 18/166
He also said:
Justice is obligatory for everyone towards everyone in all situations, and injustice is not permissible in the slightest in any situation, to such an extent that Allah, may He be exalted, has obliged the Muslims to treat the disbelievers justly, as He, may He be exalted, says (interpretation of the meaning):
“Stand out firmly for Allah and be just witnesses and let not the enmity and hatred of others make you avoid justice. Be just: that is nearer to piety”
The believers used to attack the disbelievers by Allah's command, then Allah, may He be exalted, said, explaining the matter: Do not let your hatred for the disbelievers make you not treat them justly; rather be just towards them, for that is nearer to piety.
End quote from Majmoo‘ al-Fataawa, 30/339
Shaykh Ibn Baaz (may Allah have mercy on him) said:
Allah, may He be glorified, has made it obligatory for the Muslims to be just towards their enemies, and He has commanded them to establish justice with anyone who disputes with them. And He has forbidden them to allow hatred of the people to make them fail to be just towards them. He, may He be glorified and exalted, tells us that justice towards enemies and friends alike is closer to piety. What this means is that justice towards all people, friends and enemies alike, is closer to guarding against the wrath and punishment of Allah.
End quote from Majmoo‘ Fataawa Ibn Baaz, 2/182
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked:
If a disbeliever and a Muslim come to us at work, because they need something from us, and the disbeliever comes before the Muslim, should we give precedence to the Muslim over the disbeliever, or give precedence to the one who came first?
No; start with the one who came first. This is only fair. Hence the scholars said: If a disbeliever and a Muslim refer to the qaadi (judge) for judgment, should he treat the Muslim better than the disbeliever?
No, he should not give him favorable treatment; rather he should treat them equally. He should not speak to the Muslim gently and to the disbeliever harshly; this is not permissible. Judging between people must be done on the basis of justice. If you do that and you are just, that may be a cause of the disbeliever becoming Muslim.
End quote from Liqa’ al-Baab al-Maftooh, 79/17
See also the answer to question no. 111911
And Allah knows best.