Codeception: How to use _bootstrap.php for sharing data?

Created on 19 Nov 2013  路  13Comments  路  Source: Codeception/Codeception

I get that _bootstrap.php is used for sharing data between tests and also autoloading. But how do you actually use it to share data?

When I put variables in it, I can't seem to access it in any of the tests. I'm talking about unit tests specifically.

I can't declare classes inside of them since this causes a class redeclaration error since it's ran multiple times.

Is this where you put fixtures? So I tried adding data to the fixtures in _bootstrap.php, but I couldn't get the fixtures out of them inside my unit tests.

Where do I go about implementing fixtures that are just an array of data, nothing related to the database.

Furthermore is there any reason to put in Composer's autoloader into the _bootstrap. The tests seem to work fine autoloading properly without the need to add Composer's autoloader.

Docs

Most helpful comment

in bootstrap

Fixtures::add('data', $data);

in unit test

Fixtures::get('data')

and do cleanup in helper...

Is that how you doing that?

All 13 comments

I can't declare classes inside of them since this causes a class redeclaration error since it's ran multiple times.

You can use require_once inside of it. And place your declared classes somewhere into _data dir.

When I put variables in it, I can't seem to access it in any of the tests. I'm talking about unit tests specifically.

Yep, variables are available only in Cept tests.

Is this where you put fixtures? So I tried adding data to the fixtures in _bootstrap.php, but I couldn't get the fixtures out of them inside my unit tests.

Same as above.

Oh my, looks like I cleaned up a great part of documentation on using fixtures in unit tests. Anyway, you should put data to global registry. You can use this file for that: https://github.com/Codeception/Codeception/blob/1.8/src/Codeception/Util/Fixtures.php

But yes, no great solution for fixtures is implemented.

Furthermore is there any reason to put in Composer's autoloader into the _bootstrap. The tests seem to work fine autoloading properly without the need to add Composer's autoloader.

Nope. Codeception is already executed by Composer's autoloader.

I tried adding data to the Fixtures class but the Fixtures instance inside the unit tests are always empty? Is this a case of Unit tests only and it only works for Cept tests?

in bootstrap

Fixtures::add('data', $data);

in unit test

Fixtures::get('data')

and do cleanup in helper...

Is that how you doing that?

Pretty much. I however used the fully qualified namespaces.

Can you write in the documentation how you're meant to use Fixtures with regards to unit tests and not acceptance tests?

i'm getting class fixtures not found in __boostrap, how do I include it?

I'm using laravel and have codeception installed in the vendors folder, webguy installed in the app folder

I'm doing exactly what @DavertMik says (and follow already the documentation):

Fixtures::add('data', $data); in bootstrap
Fixtures::get('data') in my test

But get this error:
Class 'Fixtures' not found in /var/www/html/xxx/tests/_bootstrap.php on line 27

What I'm doing wrong? :tired_face:

Last time I used it, it only works for acceptance tests and not unit tests.

I have the exact same scenario as @agarzon, except that I _am_ running acceptance tests, not unit tests.

// _bootstrap.php
use Codeception\Util\Fixtures;
Fixtures::add('username', 'tombombadil');

// xxxCept.php
$username = Fixtures::get('username');

I am still getting the Class 'Fixtures' not found error. Am I missing a step?

Not sure if this is the proper solution, but apparently I was just missing a use statement in my cept:

// xxxCept.php
use Codeception\Util\Fixtures;
$username = Fixtures::get('username');

The error went away (as one would expect) and I had access to everything I'd defined in my bootstrap.

@drake-p My solution was to use a helper: https://github.com/leewillis77/codeception-config-helper

@DavertMik I think that content of _bootstrap.php should be updated to be more correct.
It survived almost unchanged since the initial commit: https://github.com/Codeception/Codeception/commit/111d08289a16493068c9d62b7b6c6c58daffc758#diff-18f2da2f2bb13d95f932d238c2992aa9R72

For those stumbling into this issue wondering why simple variable definitions aren't working, note that constants do work fine.

I'm new here and don't know the benefit of using Fixtures instead of constants, but simply defining a constant in _bootstrap.php makes it available to your tests. So something like:

 // _bootstrap.php
define('GREETING', 'Hello World');
// xxxCept.php
$I->haveHttpHeader('WHATSAYYOU', GREETING);
Was this page helpful?
0 / 5 - 0 ratings