Prior to human consumption of cannabis products, rigorous testing must occur to ensure the product is safe for consumers. Testing for residual solvents, heavy metals, and microbial contamination are all required prior to cannabis entering a dispensary.  This stringent testing is especially important for patients with medical conditions where contamination can significantly compromise their health.  Cannabis plants can accumulate mold (fungi) and bacteria in some growing conditions, from the post-harvest processing, or commonly from the environment when plants are cultivated outdoors. 
Testing for microorganisms involves methods to determine if bacteria or fungi are contaminating the Cannabis that will be sold or extracted. Depending on the state in which lab testing occurs, the extent of microbial testing and the specific microbes identified as dangerous may change. For instance, Oregon uses water activity (as of 2019) as an alternative for microbial testing because microbes need a certain threshold of water to survive.
The conventional method to detect the presence of microbes is using a plate counting method. This method involves testing a Cannabis sample on microbial growth medium then counting the different microbial colonies that develop over a given timeframe.  However, this method is not specific to harmful microbes and there will likely be additional non-pathogenic strains present on the Cannabis. Further, this method requires either a highly trained microbiologist to identify the cultures based on morphology or further genetic testing to confirm the species of the microbe present.  Lastly, different microbes grow at different rates, and 99% of soil microbes are not able to be cultured at all.  Because of these limitations, genetic methods are becoming more prevalent.
Polymerase chain reaction (PCR) or the newer quantitative PCR (qPCR) are quickly becoming the gold standard for microbial testing.  Using PCR, a specific genetic marker can be searched for in the Cannabis sample; if the microbe is present in the sample, that genetic marker will be amplified and detected. If the microbe is not present in the sample, nothing will be amplified and therefore be negative. PCR can allow identification of the presence or absence of specific microbes, but it cannot tell you a reliable quantification of how much microbial contamination was present in the sample.  qPCR is a similar concept as PCR but is more sensitive and utilizes a fluorescent probe allowing researchers to determine the level of amplification that is occurring, and thus allowing quantification of the microbial contamination in the sample. One drawback of PCR-based genetic methods of microbial identification is the potential to amplify dead cells in which the DNA can still be amplified but the microbes are unviable.  There are methods, however, to overcome this drawback such as before-after culture qPCR and live-dead qPCR. 
References: Seltenrich N. Cannabis contaminants: Regulating solvents, microbes, and metals in legal weed. Environ Health Perspect. 2019;127(8):82001. doi:10.1289/EHP5785. [Impact Factor: 8.38; Times Cited: 7 (Semantic Scholar)]
 Ruchlemer R, et al. Inhaled medicinal cannabis and the immunocompromised patient. Supportive Care in Cancer: Official Journal of the Multinational of Supportive Care in Cancer. 2015;23(3):819-22. [Impact Factor: 2.635; Times Cited: 36 (Semantic Scholar)]
 Pham VH, Kim J. Cultivation of unculturable soil bacteria. Trends Biotechnol. 2012;30(9):475-84. [Impact Factor: 14.343; Times Cited: 274 (Semantic Scholar)]
 Garibyan L, Avashia N. Research techniques made simple: Polymerase chain reaction. J Invest Dermatol. 2013;133(3):1-4. doi:10.1038/jid.2013.1. [Impact Factor: 7.143; Times Cited: 27 (PubMed)]
 Zeng D, et al. Advances and challenges in viability detection of foodborne pathogens. Front Microbiol. 2016;7:1833. doi:10.3389/fmicb.2016.01833. [Impact Factor: 4.235; Times Cited: 45 (Semantic Scholar)]
 Amirault B. “Solving qPCR’s Live-Dead Problem,” Terpenes & Testing Magazine, July-Aug 2020.