-3

I would like to add integration testing/automated acceptance testing for a mobile app calling a registration API from the backend. I will register a sample user on the test feature then delete the user on tearDown. Something like this:

test('registration test'){
   RegistrationRobot registrationRobot = RegistrationRobot();
   registrationRobot.inputEmail('sample@email.com');
   registrationRobot.inputPassword('SAMPLEPW');
   registrationRobot.tapSignUpButton();
   assert(NextScreen, isPresent);
}

tearDown(){
   deleteUser('sample@email.com');
}

Is this considered bad practice?

1 Answers1

1

I'm guessing that you're asking whether using tearDown to remove the user is bad practice. There are indeed a few issues/alternative approaches:

  1. The email address is currently duplicated. If either the test or the teardown changes your code will do the wrong thing, and probably fail. Instead, both instances of that string should be tied to a single variable.
  2. Consider why you are deleting the user after the test. If it's because you intend to reuse the credentials between tests, that's an anti-pattern: you can't run the tests in parallel (a good idea to do early to detect test interactions), and there are (hidden!) interactions between tests which are likely to make them brittle. You can instead use a "faker" library or a simple random string generator to create random email addresses which are vanishingly unlikely to ever collide. Related example.
  3. Deleting the user takes time, which is probably better spent running other tests unless you are running some gigantic test suite that's likely to exhaust storage on your machine. Instead simply overwrite the database at the start of each test run, and you should save a bunch of time.
  4. As a minor side note, "example.com" or "example.org" are the canonical example domains. "email.com" is probably used by an actual email service, and you don't want your test emails accidentally reaching random people.
l0b0
  • 11,014
  • 2
  • 43
  • 47
  • Thanks for the answer. I edited my question, the reason I want to delete the user is that I'm calling the registration API from a mobile device, so I can't access the database directly – xitnesscomplex Mar 16 '22 at 07:38
  • @xitnesscomplex, this answer still applies to your question even though you are calling it from a mobile device. Can you clarify what you mean by "is this considered bad practice?" – Greg Burghardt Mar 16 '22 at 11:57