I was surprised to find out that Drupal's simpletest framework does not have a built in function to create a user with a role. After searching around the net I came across this blog post which helped me: http://mediatribe.net/en/node/49
Here is how I did it:
/**
* Creates a user with the give role.
**/
public function drupalCreateUserWithRole($role) {
// Get all of the roles in the system.
$roles = user_roles();
// Find the index for the role we want to assign to the user.
$index = array_search($role, $roles);
// Get the permissions for the role.
$permissions = user_role_permissions(array(array_search($role, $roles) => $role));
// Create the user with the permissions.
$user = $this->drupalCreateUser(array_keys($permissions[$index]));
// Assign the role.
$user->roles[$index] = $role;
// Return the user we have created.
return user_save($user);
}
Nice syntax version here: https://gist.github.com/3677218