Is there any way to insert f-curves keyframes by script?
You can find the data paths by right-clicking on the property and choosing 'Copy Data Path'. This gives you something like pose.bones["Bone.001"].rotation_euler.

With those you can call keyframe_insert directly on the armature object:

for ai in range(3):
    armature.keyframe_insert('pose.bones["Bone.001"].rotation_euler', ai)


This creates a keyframe for array indices 0, 1, and 2, for X, Y, and Z angles. If you want to use Quaternions, make sure you also use array index 4.

The keyframe_insert function will create a keyframe with the current value of the property (in this case the rotation); if you want to store another value, first change the property itself. You can also pass frame=f if you want to store the current value at a different frame.

By calling bpy.data.actions.new(name = 'Test') you always create a new action, which may not be what you want. Calling keyframe_insert directly on the armature will automatically create a new action if needed, so that's quite handy.

For more info, see the documentation for keyframe_insert.