Execute script WITH GUI 'Context' object has no attribute 'active_object'
When calling blender from the CLI or script with arguments, it will "perform" each argument in the order it is presented, this can be important with many tasks.

What that means, is that blender -P script.py file.blend will run the given script and then open the blend file, while blender file.blend -P script.py will open the blend file and then run the script. In the second example you don't need to run bpy.ops.wm.open_mainfile and won't have the issue you are trying to solve.

That also means, your addon test example can be reduced to a single line and passed to blender without using a script.

blender file.blend --python-expr 'import bpy; bpy.ops.mesh.operator_to_test()'


Rather than combining a string to pass to os.system() I would suggest using subprocess.call() which uses a list for the arguments, preventing the need to quote and escape... effectively the list becomes sys.argv

As you are doing an automated addon test, consider using --factory-startup and --addon to control what addons are enabled. You could do one run with your normal startup file and another with factory and your addon or simply ensure your addon is enabled without having to change your startup file.

To test an addon with multiple files -

from glob import glob
from subprocess import call

for blendFile in glob('*.blend'):
    arglist = [
    'blender',
    '--factory-startup',
    '--addons',
    'addon_test',
    blendFile,
    '--python-expr',
    'import bpy; bpy.ops.mesh.operator_to_test()'
    ]
    call(arglist)


Or you can loop through multiple addons and test one at a time -

from subprocess import call

for addon in ['addon_test1', 'addon_test2']:
    arglist = [
    'blender',
    '--factory-startup',
    '--addons',
    addon,
    'test.blend',
    '--python',
    addon + '_test.py'
    ]
    call(arglist)