A pregnancy test checks for the presence of the "pregnancy hormone" hCG in the blood or urine.
HCG is an abbreviation and the full name is: "Human Chorionic Gonadotropin", the "pregnancy hormone" which is being produced by the placenta as soon as implantation happens about one week after fertilization and ovulation. As soon as the embryo implants and the placenta attaches to the uterine lining, hCG enters the circulation as eventually is excreted into the urine.
One single hCG value does not tell you much about the viability of the pregnancy and there is a wide range of normal hCG levels in pregnancy. A normal hCG rise over several days prior to 6 weeks of the pregnancy usually indicates a viable pregnancy. In general, the best indication of a healthy pregnancy other than a normal rise of hCG is a good fetal heartbeat after 6-7 weeks.
There is a wide range of normal hCG levels and values and the values are different in blood serum or urine.
Urine hCG levels are usually lower than serum (blood) hCG levels.
It's main function is to support the corpus luteum producing progesterone when conception occurs. hCG can be detected both in the urine and in the blood. In blood it's detected several days earlier than in the urine.