A paternity test can be completed in utero, well before the baby is born but it usually requires an invasive procedure like an amniocentesis or CVS chorionic villus sampling.
Though the process comes with certain risks to the baby, in cases where the paternity needs to be determined before the child is born, there are two options for prenatal paternity testing - amniocentesis and CVS.
After the 14th week of gestation, mothers can request an amniocentesis for paternity testing. The amniotic fluid carries the cells of the baby and these cells can be used to strip out the DNA needed for a paternal match. An amniocentesis can be done any time during pregnancy after 15 weeks, though most are done between 15 and 20 weeks.
In utero paternity testing is expensive and may not be covered by medical insurance. There is also the risk of damage to the fetus or the amniotic sac. In rare cases, the fluid will continue to leak from the sac and the baby will need to be delivered or you have a miscarriage. Most often, the court system will need to be involved for a doctor to consent to prenatal paternity testing.