I noticed that when you create a user in a test class that the username must be unique in the real production environment. Most of my test classes are creating a user to then run the test method as.

The problem arises from the fact that you can create a test class, deploy it to production, and then have it break by someone else, coincidentally using that same username. Is there a best practice to avoid this besides trying to create long random usernames? Should we query for a random user in the org of that profile?

  • Why not use [email protected] or [email protected] Commented Feb 21, 2017 at 17:37
  • That would seem to increase the chance that an admin would create a test user with that name. Since we use communities there are a lot of test community users that have our company's domain.
    – brezotom
    Commented Feb 21, 2017 at 17:41
  • 1
    String username = string.valueOfGmt(System.now()); Commented Feb 21, 2017 at 17:44
  • 1
    I mock my user names starting with an underscore e.g. [email protected]
    – cropredy
    Commented Feb 21, 2017 at 18:08
  • 1
    Remember that usernames just need to be in a form of a email address it doesn't need to be a resolvable email address. So that unleashes a host of possibilities. I would keep the domain as yours because as you may have found out someone created a user with [email protected] as a username. By using your own domain will ensure that others will not take it. For example I would never create a user with @brezo.com, but you might. Commented Feb 21, 2017 at 18:19

2 Answers 2


With following combination to generate unique Username, there were no errors while using runAs with deployments:

public static User createTestUser(Id roleId, Id profID, String fName, String lName) {
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now())
        .replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
    User tuser = new User(  firstname = fName,
                            lastName = lName,
                            email = uniqueName + '@test' + orgId + '.org',
                            Username = uniqueName + '@test' + orgId + '.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = uniqueName.substring(18, 23),
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = profId,
                            UserRoleId = roleId);
    return tuser;

I had referred to some blog or community answer for this resolving issue way back. Don't have link of original post, but noticed that its now mentioned in Salesforce documentation here.


You can use System.now() as username with some prefix like

String username = 'Test'+string.valueOfGmt(System.now()); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .