Unittest - running Maya code seperately works as expected, running through unittest causes failure

I have the below code for testing some code I have implemented for duplicating an attribute
I’ve had to run a part that involves selecting a part of the channelbox, as the code I am currently relying on is pm.channelBox('mainChannelBox', query=True, selectedMainAttributes=True)

When I run this code manually, it works perfectly, I can select it all in the script editor, run all, and it works as expected. Running through Unittest its getting cranky, although it is still completing what needs to be done, its failing the test first, then completing everything

So my main question is, is there a way to either get the channelbox information that doesn’t involve querying the UI (as I think thats the problem) or is there a way to force maya to complete its current commands before it continues on in code, so that way the test will only assert itself when the rest of the commands have actually completed

(I have also tried a cmds.refresh, and a scriptjob, waiting for idle and both have failed me :frowning:)

def test_duplicate_selected_attrs(self):
        pm.playbackOptions(minTime=0, maxTime=3)
        times = [1, 2, 3]
        values = [-50, 50, 0]
        attr_name = 'translateX'

        node, anim_curve = create_node_with_anim(times=times, values=values, attr=attr_name)

        pm.select(node)

        def select_channel():
            cmds.channelBox('mainChannelBox', e=True, select=['{}.{}'.format(node, attr_name)])
            base.duplicate_selected_attrs()

        cmds.evalDeferred(select_channel)
       
        scene_curves = pm.ls(type='animCurve')
        self.assertEqual(len(scene_curves), 12)