Order of execution of unit tests

Asked

Viewed 165 times

0

Is there any way to define the order of execution of the test methods ?

When I put the function code testSelectDaInsercao inside testInsercaoDeDados the tests are executed successfully, but this way the test fails. I believe it is due to the execution order.

I looked in the documentation and saw only something about dependsand made it clear that this would not change the order. I already reversed the order of the methods, but the test also failed !

use Application\Conn;
use Application\operacao;
use PHPUnit\Framework\TestCase;

class PHPTest extends TestCase 
{
    private $conn;

    public function setUp()
    {
        $this->conn = new Conn();
        $this->conn = $this->conn->retornaConexao();
    }

    public function testInsercaoDeDados()
    {      
        $stmt = $this->conn->prepare("INSERT INTO usuario values (?, ?)");

        $stmt->bindValue(1, 'teste', \PDO::PARAM_STR);
        $stmt->bindValue(2, '123456', \PDO::PARAM_STR);

        $resul = $stmt->execute();
        $this->assertTrue($resul);  
    }

    public function testSelectDaInsercao()
    {
        $resul = $this->conn->query("SELECT count(usuario.nome) as id 
                                    FROM usuario WHERE nome = 'teste' 
                                    AND senha = '123456'");

        $resul = $resul->fetch(\PDO::FETCH_ASSOC);

        $resul = (int) $resul['id'];

        $this->assertEquals(1, $resul, 'erro ao comparar insercao'); 
    }

    public function tearDown()
    {           
        $this->conn->query("truncate table usuario");
    }

}

using depends /** * @depends testInsercaoDeDados **/

  • show how you do it...

  • I don’t understand the question, you want to see the error message ?

  • if you have error message, also show...

  • to run you have to use phpunit, ta ai the error

  • of the same thing

  • so I put {@depends} as "comment". I did not put the return, based on the documentation it would not solve. It says : . Such dependencies do not define the order in which the test methods should be executed.https://phpunit.de/manual/current/pt_br/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies

  • I’ve already edited the question

  • so, qnd I put in the same function works, which means it’s entering. As for the id, it’s auto_increment. If you were not entering the first test you would not have passed, since it would return false.

Show 3 more comments

2 answers

1

The problem is in the method tearDown:

public function tearDown()
{           
    $this->conn->query("truncate table usuario");
}

This method is always executed after each test, whether valid or failed. That is, when the test testInsercaoDeDados is executed, information is recorded, but when the test is completed, the method tearDown is executed by clearing the records. When the test testSelectDaInsercao is executed, there will be no records.

To get around this problem, you can do the truncate of the table in the method tearDownAfterClass, as this will be performed only when ALL tests are run, thus ensuring that records are persisted while tests are running.

  • they were already using the same connection.... However after making these modifications and taking that line'$this->Conn->query("truncate table user");' from the tearDown method and adding in the testSelectDaInsertion the test worked.... a bit strange but ....

  • 1

    It won’t work because of the tearDown

1


Your problem is not in the order of execution, it is in the method tearDown.

Phpunit will execute the methods setUp and tearDown after each execution class method.

As his tearDown is giving a truncate in the whole table (which is correct), when running the next test of this class it will start with the table reset again.

To solve this, two approaches can be taken:

Unify both tests into one:

use Application\Conn;
use Application\operacao;
use PHPUnit\Framework\TestCase;

class PHPTest extends TestCase 
{
    private $conn;

    public function setUp()
    {
        $this->conn = new Conn();
        $this->conn = $this->conn->retornaConexao();
    }

    public function testInsertAndRecoverFromDatabase()
    {      
        $stmt = $this->conn->prepare("INSERT INTO usuario values (?, ?)");

        $stmt->bindValue(1, 'teste', \PDO::PARAM_STR);
        $stmt->bindValue(2, '123456', \PDO::PARAM_STR);

        $result = $stmt->execute();
        $this->assertTrue($result);

        $selectResult = $this->conn->query("SELECT count(usuario.nome) as id 
                                    FROM usuario WHERE nome = 'teste' 
                                    AND senha = '123456'");

        $selectResult = $selectResult->fetch(\PDO::FETCH_ASSOC);

        $insertedId = (int) $resul['id'];

        $this->assertEquals(1, $insertedId , 'erro ao comparar insercao'); 
    }    

    public function tearDown()
    {           
        $this->conn->query("truncate table usuario");
    }    
}

In place of setUp and tearDown, use setUpBeforeClass and tearDownAfterClass. These are two static methods that run only in the construction of the entire test class. Combined with the @depends you can ensure that both tests are run.

use Application\Conn;
use Application\operacao;
use PHPUnit\Framework\TestCase;

class PHPTest extends TestCase 
{
    private static $conn;

    public static function setUpBeforeClass()
    {
        static::$conn = (new Conn())->retornaConexao();
    }

    public function testInsertIntoDatabase()
    {      
        $stmt = static::$conn->prepare("INSERT INTO usuario values (?, ?)");

        $stmt->bindValue(1, 'teste', \PDO::PARAM_STR);
        $stmt->bindValue(2, '123456', \PDO::PARAM_STR);

        $result = $stmt->execute();
        $this->assertTrue($result);

        $selectResult = static::$conn->query("SELECT count(usuario.nome) as id 
                                    FROM usuario WHERE nome = 'teste' 
                                    AND senha = '123456'");

        $selectResult = $selectResult->fetch(\PDO::FETCH_ASSOC);

        $insertedId = (int) $resul['id'];

        $this->assertEquals(1, $insertedId , 'erro ao comparar insercao'); 
    }    

    /** @depends testInsertIntoDatabase */
    public function testSelectPreviousInsertedRegister()
    {
        $resul = $this->conn->query("SELECT count(usuario.nome) as id 
                                    FROM usuario WHERE nome = 'teste' 
                                    AND senha = '123456'");

        $resul = $resul->fetch(\PDO::FETCH_ASSOC);

        $resul = (int) $resul['id'];

        $this->assertEquals(1, $resul, 'erro ao comparar insercao'); 
    }

    public static function tearDownAfterClass()
    {           
        static::$conn->query("truncate table usuario");
    }    
}
  • ah yes, now I understood this tearDown. I had been wrong about its use

Browser other questions tagged

You are not signed in. Login or sign up in order to post.