<p>I needed to define the namespace. In composer.json:</p>
<pre><code>&quot;autoload&quot;: {
    &quot;psr-4&quot;: {
        &quot;myname\\tests\\fixtures&quot;: &quot;tests/fixtures&quot;
    }
}
</code></pre>
<p>and then</p>
<pre><code>composer install
</code></pre>
<p>That uncovered some other problems with the above code (to anyone else who's trying to figure out fixtures, looking through the Craft repo's own tests directory is helpful), but that's separate from this question.</p>

CC BY-SA 4.0
2020-06-26T01:44:04.667Z
2020-06-26T14:00:53.973Z
2020-06-26T14:00:53.973Z
henry
<p>Add the following lines to <code>./tests/_bootstrap.php</code> this worked for me.</p>
<pre><code>
use craft\test\TestSetup;
use Codeception\Util\Autoload; //this line

[...contants define...]

Autoload::addNamespace('myname\tests\fixtures', __DIR__ . DIRECTORY_SEPARATOR . 'fixtures'); //this line

$devMode = true;

TestSetup::configureCraft();
</code></pre>

CC BY-SA 4.0
2021-02-06T03:26:17.483Z
2021-02-06T03:26:17.483Z
Leo Leoncio
<p>I've set up a fixture, but Codeception can't find it. What am I missing or doing wrong?</p>
<pre><code>% php vendor/bin/codecept run functional
...                                                                    
  [Error] Class 'myname\tests\fixtures\MyFixture' not found
</code></pre>
<p>In the sample code the namespaces are under <code>myname</code> but I've also tried using just <code>tests\fixtures</code>, <code>tests\unit\fixtures</code>, and <code>myname\tests\unit\fixtures</code>.</p>
<p>Thinking it's something to do with the namespace or most likely something to do with <a href="https://docs.craftcms.com/v3/testing/testing-craft/fixtures.html" rel="nofollow noreferrer">the Craft fixtures docs</a> saying</p>
<blockquote>
<p>Fixtures…can be defined in the <code>fixturesMethod</code> defined in the <code>codeception.yml</code> file.</p>
</blockquote>
<p>My codeception.yml doesn't have <code>fixturesMethod</code>. I can imagine that's a way of doing some of <a href="https://www.yiiframework.com/doc/guide/2.0/en/test-fixtures#configure-command-globally" rel="nofollow noreferrer">this</a> but haven't found any documentation describing what that configuration should look like.</p>
<p>I've also tried loading the fixture on the command line</p>
<pre><code>% ./craft fixture/load ImageAssetFixture
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: &quot;tests\unit\fixtures&quot;. Please, check your FixtureController::namespace parameter'


% ./craft fixture/load ImageAssetFixture --namespace='myname\tests\fixtures'
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: &quot;myname\tests\fixtures&quot;. Please, check your FixtureController::namespace parameter'
</code></pre>
<p>Here's the setup:</p>
<p><code>tests/functional/AssetFixtureCest.php</code></p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

use FunctionalTester;
use myname\tests\fixtures\MyFixture;

class AssetFixtureCest
{
    public function _fixtures()
    {
        return [
            'myFixture' =&gt; [
                'class' =&gt; MyFixture::className(),
                'dataFile' =&gt; codecept_data_dir() . 'my-fixture.php',
            ],
        ];
    }

    public function testMyFixture(FunctionalTester $I)
    {
        $myFixture = $I-&gt;grabFixture('myFixture');
        codecept_debug($myFixture);
    }
}
</code></pre>
<p><code>tests/fixtures/MyFixture.php</code></p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace myname\tests\fixtures;

use Craft;
use craft\test\fixtures\elements\...;

class MyFixture extends ...
{
    /**
     * {@inheritdoc}
     */
    public $dataFile = __DIR__ . '/data/my-fixture.php';
}
</code></pre>
<p><code>test/fixtures/data/my-fixture.php</code></p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

return [
    ...
];
</code></pre>

<tests>
Namespace error with fixture (solved)
# Namespace error with fixture (solved)

(Asked by: username_0 on 2020-06-25)

I've set up a fixture, but Codeception can't find it. What am I missing or doing wrong?
% php vendor/bin/codecept run functional
...                                                                    
  [Error] Class 'myname\tests\fixtures\MyFixture' not found

In the sample code the namespaces are under myname but I've also tried using just tests\fixtures, tests\unit\fixtures, and myname\tests\unit\fixtures.
Thinking it's something to do with the namespace or most likely something to do with the Craft fixtures docs saying

Fixtures…can be defined in the fixturesMethod defined in the codeception.yml file.

My codeception.yml doesn't have fixturesMethod. I can imagine that's a way of doing some of this but haven't found any documentation describing what that configuration should look like.
I've also tried loading the fixture on the command line
% ./craft fixture/load ImageAssetFixture
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: "tests\unit\fixtures". Please, check your FixtureController::namespace parameter'


% ./craft fixture/load ImageAssetFixture --namespace='myname\tests\fixtures'
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: "myname\tests\fixtures". Please, check your FixtureController::namespace parameter'

Here's the setup:
tests/functional/AssetFixtureCest.php
 [
                'class' => MyFixture::className(),
                'dataFile' => codecept_data_dir() . 'my-fixture.php',
            ],
        ];
    }

    public function testMyFixture(FunctionalTester $I)
    {
        $myFixture = $I->grabFixture('myFixture');
        codecept_debug($myFixture);
    }
}

tests/fixtures/MyFixture.php

test/fixtures/data/my-fixture.php

**Tags:** tests


---

## 2 answers

### Answer by username_0 on 2020-06-26. Score: 1 | Accepted answer

I needed to define the namespace. In composer.json:
"autoload": {
    "psr-4": {
        "myname\\tests\\fixtures": "tests/fixtures"
    }
}

and then
composer install

That uncovered some other problems with the above code (to anyone else who's trying to figure out fixtures, looking through the Craft repo's own tests directory is helpful), but that's separate from this question.


---

### Answer by username_1 on 2021-02-06. Score: 1

Add the following lines to ./tests/_bootstrap.php this worked for me.

use craft\test\TestSetup;
use Codeception\Util\Autoload; //this line

[...contants define...]

Autoload::addNamespace('myname\tests\fixtures', __DIR__ . DIRECTORY_SEPARATOR . 'fixtures'); //this line

$devMode = true;

TestSetup::configureCraft();


---

