Those who would like to rule out a possible HIV infection should get tested, at the earliest, three months after the last risk situation. Only then can the HIV test reliably rule out an HIV infection.
The reason: the test does not detect the HIV virus itself in the blood but rather the antibodies that the immune system forms to fight the virus. For some people, it takes up to three months for these antibodies to form and become detectable.
So a “negative” test result shortly after risk of infection does not necessary mean that the person is not infected with HIV. It only indicates that there are still no antibodies in the blood.
The time it takes for antibodies to form can vary widely from person to person. That is why it is possible for an HIV test to detect an HIV infection even before the three months have passed. Though, an infection can only be definitively ruled out after three months have passed.