Magnesium is essential for hormone production, and testosterone is no exception. At this point we can safely say that low magnesium levels, do result in lowered testosteorne. It also effects melatonin which you need for sleep and a host of other hormonal functions so this shouldn't be a surprise. The question of course is whether or not magnesium can raise testosterone levels beyond the daily minimum you need to function.
And, so far it seems the answer is in fact yes, magnesium is one of the most well researched minerals when it comes to it's effects and most of the studies do in fact seem to show a positive trend even in men who were not otherwise deficient. It's even been shown to effect womens testosterone levels, and yes women do need testosterone too, and low test levels can cause problems there, not just for men.
And we'll come on to a few of those now.
Magnesium and Testosterone: The Studies
Low Magnesium and Low Testosterone Levels
As we mentioned there's quite a few studies that show low magnesium and low testosterone levels have a strong correlation, for example this study  found a low correlation between parotid saliva testosterone and magnesium levels, and further studies such as the one in the journal "Expert Review of Endocrinology & Metabolism" showed the same results.  A lot of studies seem to back this up, and the studies that show the contrary are usually ones done on animals, meaning they have next to now use. The interesting thing is that even mild magnesium deficencies can lower testosterne levels. Further studies also magnesium was important for older men's testosterone levels, with those who had more magnesium in their diets showing notably higher serum testosterone levels. 
Can Magnesium Raise Testosterone in Healthy Men?
So, what about men who already get enough magnesium, again we have quite a few studies that suggest it can in fact have an impact. And it actually even seems to work more for individuals who train, with studies showing supplementation with magnesium increases free and total testosterone values in sedentary men as well as in athletes. And as we mentioned the increases were higher in the athlete group than in men who didn't exercise. 
Magnesium supplementation also resulted in an increase in DHEA as well as testosterone levels, meaning it has a double effect for people looking to build lean muscle mass which is one of the main reasons people look to supplement magnesium in relation to test production.
Other Studies That Link Magnesium and Testosterone
On top of that it's also been shown to increase sperm health and fertility as well as prostate health, all of those are connected to testosterone. With studies going all the way back to the 80s and being consistently replicated. [6,7]
So How Does Magnesium Raise Testosterone?
Aside from it's use in actually manufacturing testosterone itself, the reason magnesium makes such a big difference utimately comes down to the difference between free and total testosterone. Approximately 70% of testosterone in the body is bound to SHBG, and only 1-2% is "free" or bioavailable, meaning you actuall use it. Magnesium weakens the bond between SHBG and testosterone, increasing the bioavailability of even the bound testosterone. This mechanism is particularly relevant for men, as it enhances the overall effectiveness of testosterone in the body. 
How Much Magnesium Should I Take For Optimal Testosterone Production?
According to most of the recent scientific literature, the optimal daily dose of magnesium for testosterone production is around 450mg. It is recommended to consume some extra calcium as well, to maintain a correct proportion in the body or you can use too much calcium in the various conversion processes. It's also important not to take more than 3grams as this doesn't have any benefit and tends to result in particularly bad diarhea.
It's also important to make sure you take the right type of magnesium, some forms such as magnesium oxide are not very bioavailable, and will once again result in stomach problems and seeing as your body won't be able to absorb it, you won't actually do any good either.
1 - Kuraner, T., M., S., Beksac., Kadriye, Kayakırılmaz., Cağlayan, F., Onderoğlu, Ls., Hilal, Özgüneş. (1991). Serum and parotid saliva testosterone, calcium, magnesium, and zinc levels in males, with and without periodontitis.. Biological Trace Element Research, doi: 10.1007/BF02990358
2 - Michał, Wrzosek., Jakub, Woźniak., Dariusz, Włodarek. (2020). The causes of adverse changes of testosterone levels in men.. Expert Review of Endocrinology & Metabolism, doi: 10.1080/17446651.2020.1813020
3 - Marcello, Maggio., Gian, Paolo, Ceda., Fabrizio, Lauretani., C., Cattabiani., E., Avantaggiato., S., Morganti., F., Ablondi., Stefania, Bandinelli., Ligia, J., Dominguez., Mario, Barbagallo., Giuseppe, Paolisso., Richard, D., Semba., Luigi, Ferrucci. (2011). Magnesium and anabolic hormones in older men. International Journal of Andrology, doi: 10.1111/J.1365-2605.2011.01193.X
4 - Vedat, Çinar., Yahya, Polat., Abdulkerim, Kasim, Baltaci., Rasim, Mogulkoc. (2011). Effects of Magnesium Supplementation on Testosterone Levels of Athletes and Sedentary Subjects at Rest and after Exhaustion. Biological Trace Element Research, doi: 10.1007/S12011-010-8676-3
5 - Claire, André., Alain, Berthelot., Jean, François, Robert., Mireille, Thomassin., Yves, Claude, Guillaume. (2003). Testimony of the correlation between DHEA and bioavailable testosterone using a biochromatographic concept: effect of two salts.. Journal of Pharmaceutical and Biomedical Analysis, doi: 10.1016/S0731-7085(03)00428-X
6 - Birsen, Aydemir., Ali, Riza, Kiziler., İlhan, Onaran., Bülent, Alıcı., Hamdi, Özkara., Mehmet, Can, Akyolcu. (2007). Impact of Testosterone, Zinc, Calcium and Magnesium Concentrations on Sperm Parameters in Subfertile Men. doi: 10.1063/1.2733547
7 - Gérard, Sinquin., Robert, Morfin., J.-F., Charles., H.H., Floch. (1982). Testosterone metabolism by homogenates of human prostates with benign hyperplasia: effect of tissular concentrations of zinc, magnesium and copper.. Journal of Steroid Biochemistry, doi: 10.1016/0022-4731(82)90632-X
8 - Erbach M, Freckmann G, Hinzmann R, Kulzer B, Ziegler R, Heinemann L, Schnell O. Interferences and Limitations in Blood Glucose Self-Testing: An Overview of the Current Knowledge. J Diabetes Sci Technol. 2016 Aug 22;10(5):1161-8. doi: 10.1177/1932296816641433. PMID: 27044519; PMCID: PMC5032951.