Codeception/Specify

Fatal Error: Call to member function CeptTest::addFailure on null

mashiox opened this issue · 2 comments

Hello.

I am working on configuring and installing Codeception alongside Yii 1.1. So far, I have been successfully able to do this, and I am working on building a few unit test examples for my team using Specify and Verify.
However, on executing the unit test suite from codeception, PHP returns a fatal error that it reports is occurring within Specify.

I am using:

  • Yii 1.1
  • Codeception 2.3
  • Specify 0.4.6
  • Verify 0.3.3

/opt/snafu/protected/tests/codeception.yml

actor: Dev
# Note the context for this config is relative to
# /opt/snafu/protected/tests/codeception
paths:
    tests: codeception/tests
    log: codeception/log
    data: codeception/tests/_data
    helpers: codeception/tests/_helpers
    output: codeception/tests/_output
    support: codeception/tests/_support
    envs: codeception/tests/_envs
settings:
    bootstrap: ../../../bootstrap.php
    colors: true
    memory_limit: 1024M
    log: true
    debug: true
modules:
    enabled: [Yii1]
    config:
        PhpBrowser:
            url: http://localhost/index.php
        Yii1:
            appPath: /opt/snafu/test.php
            url: http://localhost/test.php
            part: init
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed

/opt/snafu/protected/tests/codeception/tests/unit.suite.yml

# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.

actor: UnitTester
modules:
    enabled:
        - Yii1:
            part: init
        - Asserts
        - \Helper\Unit

/opt/snafu/protected/tests/codeception/tests/unit/FirstCest.php

<?php


class FirstCest extends \Codeception\Test\Unit
{
    use Codeception\Specify;
    
    private $adminEmail;

    public function _before(UnitTester $I)
    {
    }

    public function _after(UnitTester $I)
    {
    }

    // tests
    public function testAdminEmailParam()
    {
        $this->adminEmail = Yii::app()->params['adminEmail'];

        $this->specify("adminEmail is required", function(){
            $this->assertTrue(isset($this->adminEmail));
        });

        $this->specify("adminEmail is in email format", function(){
            $this->assertTrue(filter_var($this->adminEmail, FILTER_VALIDATE_EMAIL));
        });
    }
}

When I execute codeception from within the tests directory, this is the output that the unit tester will generate.

root@3b2cc148fa0a:/opt/snafu/protected/tests# codecept run unit
Codeception PHP Testing Framework v2.3.3
Powered by PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

Unit Tests (208) ------------------------------------------------------------------------------------------------------------------------
FirstCest: Test admin email param
Signature: FirstCest:testAdminEmailParam
Test: codeception/tests/unit/FirstCest.php:testAdminEmailParam
Scenario --

Fatal error: Call to a member function addFailure() on null in /opt/snafu/vendor/codeception/specify/src/Codeception/Specify.php on line 130



FATAL ERROR. TESTS NOT FINISHED.
Call to a member function addFailure() on null 
in /opt/snafu/vendor/codeception/specify/src/Codeception/Specify.php:130

If it would be helpful in reproducing, this is the (tagged) work space I am using, complete with a Dockerfile for duplicating the issue I am experiencing.
https://github.com/mashiox/yii-codecept/releases/tag/specify-addFailure-issue

In order to simplify this process, please ignore the developer scripts Makefile, mount.sh, and start.sh and bootstrap the container using:

# Build the image, from within the cloned repository.
docker build -t codecept-test .

# Run the codecept-test container
docker run -it codecept-test

# Grab the container's name with docker ps, then
# Shell into the container with docker exec
docker exec -it "$(docker ps --format "{{.Image}} {{.Names}}" | grep codecept-test | cut -d" " -f2)" /bin/bash

# Execute the tests --within the container--, done in one of two ways
# Either should generate the error. 
cd /opt/snafu/protected/tests
codecept run unit

codecept --config=/opt/snafu/protected/tests/codeception.yml run unit

Your test class and file should be named FirstTest instead of FirstCest .

Yikes. That was a boneheaded mistake. Thank you for helping me turn hours of frustrated working around.