Your Location is: Home > Php

Clear my database after a unexpected error in tests with phpunit

From: Slovenia View: 4711 Onivaldo 


I'm testing a API, in the setup i make a factory to create and save a user, after i request with curl.

after that a clear the user in tearDown like the example below:

private User user;
* @before
public function setup() {
   //open connection here
   $this->user = UserFactory::makeUser();
//execute test
* @after
public function tearDown() {
  //close connection here

Even though in the middle of the request I a get a error that break the test or in the tests too. I don't want to code stop, I want to execute tearDown to remove the user from my database. Any suggestions? I'm new to testing, so any ideias will help. Thx guys.

Best answer