Testing process before PCB assembly
The components are tested at the component manufacturer, and seldom tested in the ordinary assembly process. If the component supplier ships too many bad parts they will cease to be a component supplier for any reputable company. 

Specialized (and often extremely expensive) test equipment is used to test semiconductors. There are probes that make contact with the pins. Test cost is often a significant part of the component cost. Even "almost free" parts such as resistors are tested and indeed trimmed to value, using expensive equipment. 

Here is piece of automated test equipment (ATE) used for testing modern memory chips (from Advantest, a leading Japanese supplier). Naturally this sort of thing costs millions of dollars. 



Parts may be tested more than once, at die level and after packaging. Sometimes tests are skipped for the toy market and such like where the packaged chip or COB PCB will simply be discarded if the IC fails to perform well enough. Sometimes test fallouts are shipped to countries with relatively low labor costs (for example, from a factory in China to customers in South Asia) where they can be sorted into "usable" and trash. This is common with LEDs, for example, where 75% usable parts may be considered okay. That would be completely unacceptable for customers of the A grade parts, of course.