Create a new user and assign it a profile and role by querying and insert the user.
You can also cover in a different way by creating an account and a related contact. Enable account as partner user then create a user, assign it a partner profile and add contactid in user.