Search code examples
phpcodeception

Codeception Conditional Assertions with custom functions


So we got Codeception to look at a different database while testing another database in the same Cept file

What I need help with is getting the functions CanSeeInMasterDatabase and CantSeeInMasterDatabase to throw errors once the test completes.
In other words the known Codeception functions that begin with "canSee" and "cantSee" etc allow the tests to keep running in the event one of them fails and once the test is complete it will give you a little summary of what failed at the end.

In the below functions I am getting the correct error during the test (see below) but not getting a summary of errors at end of the test

←[36m SELECT * FROM tablename WHERE columnname= "1" AND field1= "data"
←[39m
←[36m Error: Database value could be found in tablename
←[39m

The end of my test looks like, expecting to see something like "there was 1 error etc etc etc"

←[37;45m PASSED
←[39;49m
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Time: 28.09 seconds, Memory: 31.50Mb
←[30;42mOK (1 test, 0 assertions) ←[0m

We have a custom file with 4 functions like so

public function seeInMasterDatabase($tableName, $conditionArray) {
    $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);

    if (empty($databaseReturn)) {
         $this->fail('Error: Database value could not be found in '  . $tableName);
    }

}

public function dontSeeInMasterDatabase($tableName, $conditionArray) {
    $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);

    if (!empty($databaseReturn)) {
        $this->fail('Error: Database value could be found in '  . $tableName);
    }

}

public function canSeeInMaster($tableName, $conditionArray) {
    $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);

    if (empty($databaseReturn)) {
        $this->debug('Error: Database value could be found in ' . $tableName);  
    }
}

public function cantSeeInMaster($tableName, $conditionArray) {
    $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);

    if (!empty($databaseReturn)) {
        $this->debug('Error: Database value could be found in ' . $tableName);  
    }
}

Solution

  • Why do you use fail in one pair of methods and debug in another?

    You haven't pasted the code of your test, but it looks like Error: Database value could be found in tablename is the output of debug method. The fail should produce expected result.

    But I recommend rewriting your helper methods to use assertEmpty, assertNotEmpty:

    public function seeInMasterDatabase($tableName, $conditionArray) {
        $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);
        $this->assertNotEmpty($databaseReturn, 'Database value could not be found in '  . $tableName);
    }
    
    public function dontSeeInMasterDatabase($tableName, $conditionArray) {
        $databaseReturn = $this->grabFromMasterDatabase($tableName, $conditionArray);
        $this->assertEmpty($databaseReturn, 'Database value could be found in '  . $tableName);
    }
    

    In this way assertions will be counted and reported correctly.

    Edit: Do not declare can methods, can and cant methods are automatically generated for all see and dontSee methods.