No, it is not necessary to be fasting to take a pregnancy test. However, if the test is to be done in urine, it is recommended to do it with the first urine of the morning so that it is more concentrated and the result is more reliable.
Read the full article on: What Are Normal hCG Hormone Levels during Pregnancy? ( 69).

Andrea Rodrigo
B.Sc., M.Sc.
Bachelor's Degree in Biotechnology from the Polytechnic University of Valencia. Master's Degree in Biotechnology of Human Assisted Reproduction from the University of Valencia along with the Valencian Infertility Institute (IVI). Postgraduate course in Medical Genetics.