12

Codeception default _bootstrap.php file states:

<?php
// Here you can initialize variables that will be available to your tests

So I wanted to initialize a variable inside of it:

<?php

$a = 5;

However, when I use it my SomeAcceptanceCept:

<?php
// ....
$I->fillField('description', $a);

I get: ErrorException: Undefined variable: a

I did var_dump in _bootstrap.php and it indeed get's ran once before acceptance tests, but variables from it are not available in my tests.

I can't seem to find any documentation on this.

I'm actually trying to initialize Faker instance to use in my tests.

Igor Pantović
  • 9,107
  • 2
  • 30
  • 43

4 Answers4

4

I find the documentation on this to be quite confusing. I was attempting to do the exact same thing to no avail. So I ended up using Fixtures by putting this in the _bootstrap.php file:

use Codeception\Util\Fixtures;
use Faker\Factory;

$fake = Factory::create();
Fixtures::add('fake', $fake);

(You could also use a separate fixtures.php file and include it in your test.)

Which allows you to get the faker object or anything else like this:

Fixtures::get('fake');

The strange thing is that if you look at the Fixtures docs it actually mentions using the Faker library to create tests data in the bootstrap file.

Jason
  • 41
  • 3
  • This is infuriating. **_bootstrap.php** might as well do nothing at all. Remember to set your namespace in all files, eg. `use Codeception\Util\Fixtures;` in both **_bootstrap.php** and your ***Cept.php**. Of course, you could just as easily run an `include` at the top of each of your ***Cest.php** files like @Jason and @Duncan both suggested. – Joel Mellon Aug 25 '15 at 21:53
  • I'll add that the one benefit sticking with **_bootstrap.php** and implementing @Jason's fix provides is the ability to easily access vars from Cepts in folders. Otherwise your includes will be messy, eg. `include('../../_bootstrap.php')` or `include('../_bootstrap.php')`. That would be a pain if you decided to refactor your test folder structure. – Joel Mellon Aug 25 '15 at 22:02
0

I do the following:

// _bootstrap.php

$GLOBALS['a'] = 5;

Then within my tests I call it like so:

 // MyCest.php

    public function testItWorks(\Tester $I)
    {
        global $a;
        $I->fillField('description', $a);
    }

I haven't done much work with Cept files but probably something similar will work.

AYTWebSolutions
  • 331
  • 4
  • 6
0

Define it like this in your bootstrap.php :

<?php
// This is global bootstrap for autoloading


define("email", "someone@abc.com");
define("password", "my_secure_pass")

Use it like this:

public function testValidLogin(AcceptanceTester $I)
{
        $I->fillField(loginPage::$username, email);
        $I->fillField(loginPage::$password, password);
        $I->click("LOG IN");
}

NOTE: no dollar signs. use as only as 'email' and 'password'

Kotie Smit
  • 646
  • 1
  • 5
  • 18
-1

I dumped all PHP variables and confirmed that any variables set in the _bootstrap.php file do not exist and are not available in the MyCept.php file. So I resorted to adding the following at the top of MyCept.php:

include '_bootstrap.php';

This makes the variables set in _bootstrap.php available. Of course.

Duncan
  • 1
  • 1