Circuit to save one battery when another battery is present
The problem with your diagram is that the Test battery will also drive the MOSFET gate high. You'll end up dropping VGS(th) across the MOSFET. Vsetup and the gate need to be isolated from Vpwr by another diode.

The simplest solution is to use a SPDT relay:







simulate this circuit &ndash; Schematic created using CircuitLab

But if you want to create the same function using transistors, you'll need a few more:







simulate this circuit

Note that I've shown the MOSFET body diodes explicitly to emphasize the fact that source and drain on M2 have been swapped.1

When Vsetup is present, Q1, Q2 and M2 are all on. Q2 keeps M1+M3 off.

When Vsetup is not present, R1 turns M1+M3 on and R3 keeps M2 off. R5 makes sure that any leakage current from Vpwr does not turn on Q1.



1 The first version of this schematic had M1 reversed, too. That's incorrect, because we need its body diode to block Vtest current unless M1 is explicitly turned on.2 As it is, there will be a constant drain from Vtest through R1 and R2 as long as Vsetup is connected. Increase their value as needed.

2 Actually, it's worse than that. If Vsetup is greater than Vtest, then it will try to charge Vtest through M1's diode. Therefore, we need a third MOSFET M3 to protect against that. Now you can see why specialized ICs have been developed to deal with this kind of application.