There are currently no way of recording macro as python script. As described below, there is a way to record python code but this would be very similar to the generated XML and challenging to maintain.
Question: What are you trying to achieve ? By understanding this, we may be able to better answer your question.
For now, I instead suggest to write self test by hand.
There are a lot of python snippets available here, and the extension wizard will create a skeleton of module including the relevant infrastructure to run the test.
Enabling macro recording
Answers in this post assumes you are talking about using QtTesting functionality that can be enabled through the settings:
QtTesting and python
All of that said, we could look into generating python code similar to what is done in Paraview (see here). The code would be similar to the one posted below:
import QtTesting
import QtTestingImage
object1 = 'MainWindow/menubar/menuFile'
QtTesting.playCommand(object1, 'activate', 'actionFileLoadServerState')
object2 = 'MainWindow/ServerStartupBrowser/connect'
QtTesting.playCommand(object2, 'activate', )
object3 = 'MainWindow/FileLoadServerStateDialog'
QtTesting.playCommand(object3, 'filesSelected', '$PARAVIEW_DATA_ROOT/Data/LoadStateMultiView.pvsm')
snapshotWidget = 'MainWindow/1pqRenderWindowManager0/SplitterFrame/MultiViewSplitter/0/Viewport'
QtTestingImage.compareImage(snapshotWidget, 'LoadStateMultiView.png', 200, 200);