Behat: PHPUnit 4.6.* global assert

Created on 10 May 2015  路  8Comments  路  Source: Behat/Behat

I have uncommented in my FeatureContext.php

require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

And I get a stack trace:

PHP Warning:  require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in /Users/me/Projects/API/features/bootstrap/FeatureContext.php
....

And I am unable to use the assertions functions

My composer.json

{
    "require": {
        "php": ">=5.3.0",
        "slim/slim": "2.*",
        "slim/extras": "2.*",
        "diversen/http-send-file": "dev-master"
    },
    "require-dev": {
        "phpunit/phpunit": "4.6.*",
        "phpspec/phpspec": "~2.0",
        "behat/behat": "2.4.*@stable"
    },
    "autoload": {
        "psr-4": {
            "": "project/classes/"
        },
        "psr-0": {
            "": "src/"
        }
    },
    "config": {
        "bin-dir": "bin"
    }
}

Most helpful comment

Found a way to make it work on some obscure thread

use PHPUnit_Framework_Assert as Assert;
// then you can use assertions as static methods of Assert, I would still prefer globals
Assert::assertEquals(2, "2", "strange equality")

All 8 comments

You don't need those require_once statements if you install phpunit with composer.

But then the assert functions cannot be found:

<?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
class FeatureContext extends BehatContext
{
    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
    }

    /**
     * @Given /^I am in a directory "([^"]*)"$/
     */
    public function iAmInADirectory($arg1)
    {
      assertEquals(2, "2", 'they are not equal');
    }

    /**
     * @Given /^I have a file named "([^"]*)"$/
     */
    public function iHaveAFileNamed($arg1)
    {
    }

    /**
     * @When /^I run "([^"]*)"$/
     */
    public function iRun($arg1)
    {
    }

    /**
     * @Then /^I should get:$/
     */
    public function iShouldGet(PyStringNode $string)
    {
    }

}

after running bin/behat

Feature: ls
  In order to see the directory structure
  As a UNIX user
  I need to be able to list the current directory's contents

  Scenario: List 2 files in a directory # features\ls.feature:7
PHP Fatal error:  Call to undefined function assertEquals() in W:\Workspace\api\features\bootstrap\FeatureContext.php on line 38
PHP Stack trace:
PHP   1. {main}() W:\Workspace\api\vendor\behat\behat\bin\behat:0
PHP   2. Symfony\Component\Console\Application->run() W:\Workspace\api\vendor\behat\behat\bin\behat:32
PHP   3. Behat\Behat\Console\BehatApplication->doRun() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:126
PHP   4. Symfony\Component\Console\Application->doRun() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\BehatApplication.php:68
PHP   5. Symfony\Component\Console\Application->doRunCommand() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:195
PHP   6. Symfony\Component\Console\Command\Command->run() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:874
PHP   7. Behat\Behat\Console\Command\BehatCommand->execute() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:257
PHP   8. Behat\Behat\Console\Command\BehatCommand->runFeatures() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\Command\BehatCommand.php:128
PHP   9. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\Command\BehatCommand.php:150
PHP  10. Behat\Behat\Tester\FeatureTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
PHP  11. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\FeatureTester.php:88
PHP  12. Behat\Behat\Tester\ScenarioTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
PHP  13. Behat\Behat\Tester\ScenarioTester->visitStep() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\ScenarioTester.php:87
PHP  14. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\ScenarioTester.php:148
PHP  15. Behat\Behat\Tester\StepTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
PHP  16. Behat\Behat\Tester\StepTester->executeStep() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:95
PHP  17. Behat\Behat\Tester\StepTester->executeStepDefinition() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:126
PHP  18. Behat\Behat\Definition\Annotation\Definition->run() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:157
PHP  19. call_user_func_array:{W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155}() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155
PHP  20. FeatureContext->iAmInADirectory() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155

Fatal error: Call to undefined function assertEquals() in W:\Workspace\api\features\bootstrap\FeatureContext.php on line 38

Call Stack:
    0.0002     646712   1. {main}() W:\Workspace\api\vendor\behat\behat\bin\behat:0
    0.0144    2981320   2. Symfony\Component\Console\Application->run() W:\Workspace\api\vendor\behat\behat\bin\behat:32
    0.0188    3550616   3. Behat\Behat\Console\BehatApplication->doRun() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:126
    0.1825   10778248   4. Symfony\Component\Console\Application->doRun() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\BehatApplication.php:68
    0.1828   10778328   5. Symfony\Component\Console\Application->doRunCommand() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:195
    0.1828   10778488   6. Symfony\Component\Console\Command\Command->run() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Application.php:874
    0.2250   13141400   7. Behat\Behat\Console\Command\BehatCommand->execute() W:\Workspace\api\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:257
    0.2255   13156328   8. Behat\Behat\Console\Command\BehatCommand->runFeatures() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\Command\BehatCommand.php:128
    0.2382   13514096   9. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Console\Command\BehatCommand.php:150
    0.2382   13514096  10. Behat\Behat\Tester\FeatureTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
    0.2411   13627080  11. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\FeatureTester.php:88
    0.2411   13627080  12. Behat\Behat\Tester\ScenarioTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
    0.2425   13655200  13. Behat\Behat\Tester\ScenarioTester->visitStep() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\ScenarioTester.php:87
    0.2433   13715192  14. Behat\Gherkin\Node\AbstractNode->accept() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\ScenarioTester.php:148
    0.2433   13715192  15. Behat\Behat\Tester\StepTester->visit() W:\Workspace\api\vendor\behat\gherkin\src\Behat\Gherkin\Node\AbstractNode.php:42
    0.2434   13715576  16. Behat\Behat\Tester\StepTester->executeStep() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:95
    0.2454   13844120  17. Behat\Behat\Tester\StepTester->executeStepDefinition() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:126
    0.2454   13844120  18. Behat\Behat\Definition\Annotation\Definition->run() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Tester\StepTester.php:157
    0.2454   13845032  19. call_user_func_array:{W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155}() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155
    0.2454   13845352  20. FeatureContext->iAmInADirectory() W:\Workspace\api\vendor\behat\behat\src\Behat\Behat\Definition\Annotation\Definition.php:155

Found a way to make it work on some obscure thread

use PHPUnit_Framework_Assert as Assert;
// then you can use assertions as static methods of Assert, I would still prefer globals
Assert::assertEquals(2, "2", "strange equality")

@iongion When PHPUnit is installed through Composer, the library is not added in the include_path. So to find the functions, you will need to give a path:

require_once __DIR__.'/../../vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';

You may need to adjust the path to the vendor folder depending of the location of your file of course

Indeed, but got confused by those two comments that are auto-generated by Behat

Closing it as is not a Behat issue itself, but it can appear for beginner Behat users as me, there are at least two ways of working with PHPUnit as explained above by @stof and myself. Hopefully all this thread will help others that might encounter the non-issue.

@iongion Behta 3 does not generate such comment anymore

@stof When i started using Behat, I followed the available info on the
documentation page, the sections is basically:

{
    "require": {
        "behat/behat": "2.4.*@stable"
    }
}

Maybe this should be updated, the docs, by default the site opens at

http://docs.behat.org/en/v2.5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beeblebrox3 picture beeblebrox3  路  8Comments

DavidGarciaCat picture DavidGarciaCat  路  8Comments

adamcameron picture adamcameron  路  3Comments

respinoza picture respinoza  路  9Comments

mdrost picture mdrost  路  4Comments