All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
First of all, you should know that giving testimony is not an obligation unless there is no other witness whose testimony is sufficient, because giving testimony is a communal obligation (not an individual one), as has been stated by the scholars.
If there is no other person to testify and the truth depends on the testimony of a specific person, then it is an obligation on him to give his testimony because the purpose is not achieved otherwise. This is the case if there is no sound excuse for him for refraining from giving his testimony. If he has a sound excuse, then he is not obliged to give his testimony.
The Kuwaiti Fiqh Encyclopedia reads:
"If it is obligatory on a person to give his testimony but he is unable to do so due to the distance (he would have to travel to testify), like if he is invited from a distance in which he is entitled to shorten the prayer, or if he will face harm in his body, his wealth, or his family, then he is not obliged to give his testimony, as Allah says (what means): {Let no scribe be harmed or any witness.} [Quran 2:282] Besides, the Prophet said, 'There should be no harm nor reciprocal harm.' A witness is not obliged to harm himself in order to benefit others. Some scholars of Fiqh also said, 'It is not obligatory to give one’s testimony if the ruler is not just.' Imaam Ahmad said 'How can I testify with an unjust ruler? I would not testify.'"
To conclude, if we presume that it is obligatory on you to give your testimony, then if the case is as you mentioned, that you fear harm, then you are not obliged to testify.
Allah knows best.