I'm trying to autoload a bunch of helpers into a test suite. The helpers are located in a folder outside of the test suite in the hopes that I can reuse them across multiple projects if needed.
This is pretty much what I have:
- helpers
- TestHelper.php
- tests
- _data
- _output
- _support
- _generated
- Helper
- Integration.php
- IntegrationTester.php
- integration
- bootstrap.php
- integration.suite.yml
- vendor
- codeception.yml
This is the bootstrap file
// bootstrap.php
<?php
\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );
This is the global config file:
// codeception.yml
bootstrap: bootstrap.php
namespace: main
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
This is the integration suite's config file:
// integration.suite.yml
actor: IntegrationTester
modules:
enabled:
- \awesome\helpers\TestHelper
- \main\Helper\Integration
This is the TestHelper:
<?php
namespace awesome\helpers;
class TestHelper extends \Codeception\Module{
public function sayHello() {
return "Hello";
}
}
As soon as I do codecept run, I get the following error:
Module \awesome\helpers\TestHelper could not be found and loaded
I'm not posting any tests because it's irrelevant, the error is raised before any test is performed as it's a config issue.
From what I understand, the global config file should run the bootstrap file before the tests are run, and the Autoload class in the bootstrap file should load the helpers in the awesome\helpers namespace, but that's clearly not happening. What am I doing wrong?
I think that you made a classical mistake and missed / before .. so you set the path to tests../helpers.
Change
\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );
to
\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "/../helpers" );
Related
When i try to add some Helper in Api.suite.yml and run build:
php vendor/bin/codecept build
, then get error:
Building Actor classes for suites: Api
In ModuleContainer.php line 102:
Module \Helper\Api could not be found and loaded
build
My Helper autogenerated with that command:
php vendor/bin/codecept generate:helper Api
directory location in attachment enter image description here
code in Api.suite.yml:
actor: ApiTester
class_name: ApiTester
modules:
enabled:
- REST:
depends: Laravel
part: Json
- \Helper\Api
config:
- Laravel:
environment_file: .env.test
step_decorators:
- \Codeception\Step\AsJson
code in Api.php:
<?php
declare(strict_types=1);
namespace Tests\Support\Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Api extends \Codeception\Module
{
}
Codeception version: Codeception 5.0.7
i tried to put other location in Api.suite.yml:
\project\tests\Support\Helper\Api
\tests\Support\Helper\Api
\Support\Helper\Api
\Helper\Api
\Api
it still doesn't work for me
I am new to Codeception. I followed the instructions on the Quick start page (https://codeception.com/quickstart) and saw the error "Module \Helper\Acceptance could not be found and loaded" on step 6.
My folder structure is
-tests
-_output
-acceptance
-functional
-Support
-_generated
-Data
-Helper
-Acceptance.php
-unit
-acceptance.suite.yml
-functional.suite.yml
-unit.suite.yml
-vendor
-codeception.yml
-composer.json
-composer.lock
The /Helper/Acceptance.php:
<?php
namespace Helper;
class Acceptance extends \Codeception\Module
{
}
acceptance.suite.yml:
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \Helper\Acceptance
step_decorators: ~
Any suggestions? Thank you in advance.
I resolved the issue. It turned out that the issue is related to the namespace. If the Acceptance.php is located under Helper folder, then the namespace should be Tests\Support\Helper.
I have set up a functional test following the tutorials at:
http://codeception.com/docs/04-FunctionalTests
http://codeception.com/docs/modules/Symfony
http://codeception.com/09-04-2015/using-codeception-for-symfony-projects.html
The main difference is that I've set up Codeception the traditional way because I don't want to mix test code with project code.
This is my functional test (I know it's not actually testing anything):
<?php
class MyFirstCest {
public function _before(FunctionalTester $I) {
}
public function _after(FunctionalTester $I) {
}
// tests
public function tryToTest(FunctionalTester $I) {
$I->amOnPage('/app/login/');
}
}
When I run the functional test I get:
[RuntimeException] Call to undefined method FunctionalTester::amOnPage
When I rebuild Codeception, I get:
Building Actor classes for suites: acceptance, functional, unit
-> AcceptanceTesterActions.php generated successfully. 0 methods added
\AcceptanceTester includes modules: PhpBrowser, \Helper\Acceptance
-> FunctionalTesterActions.php generated successfully. 0 methods added
\FunctionalTester includes modules:
-> UnitTesterActions.php generated successfully. 0 methods added
\UnitTester includes modules: Asserts, \Helper\Unit
The critical part seems to be \FunctionalTester includes modules: which is empty.
My functional.suite.yml file looks like this:
actor: FunctionalTester
modules:
- Symfony:
app_path: 'app'
environment: 'local_test'
class_name: FunctionalTester
modules:
enabled:
- Symfony2:
app_path: 'path/to/app'
var_path: 'path/to/app'
- Doctrine2:
depends: Symfony2
- \Helper\Functional
- PhpBrowser:
url: dev.hmr-app
- \AcmeBundle\Helper\Functional
where the Symfony app lives in path/to/app. I know there's a lot of junk in there, but that's because I've been experimenting, trying to get it to work.
What am I doing wrong?
I think that your problem is that you have
modules:
- Symfony:
app_path: 'app'
environment: 'local_test'
in the config.
That section is completely misplaced and it probably is causing you issues.
Please remove it and rename Symfony2 with Symfony in enabled section.
Also make sure that you are using the latest version of Codeception.
I try to configure unit-tests suite in Codeception. When I run suite Codeception fail with error Fatal error: Cannot re-declare class rest\tests\UnitTester. With debugger I located places where it loads class first and second time.
First time Codeception load Actor in Codeception\Test\Loader\Gherkin during adding steps to loader.
Second time it initialize Actor instance for class with tests. In second place this class must be already included, but again loader is triggered. Here error appear.
Class is loaded with Codeception loader. At first I think that it was caused by namespace, but UnitTester is only one class in namespace. My .yml files a listed below. I would be very grateful for help.
codeception.yml:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Yii2:
configFile: 'config/test-local.php'
unit.suite.yml:
class_name: UnitTester
modules:
enabled:
- Yii2
- Asserts
I found a reason of this problem. My codeception.yml for this tests missing namespace param when classes in test folder was under namespace. This caused my problem.
I am using codeception 2.2.5 aand Yii 1.1.4
I have the issue with the Yii1 module. I have it configured for functional and unit tests
Here is the functional.suite.yml
class_name: FunctionalTester
modules:
enabled:
- Yii1:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
part: init
- PhpBrowser:
url: 'http://localhost/index-test.php'
- \Helper\Functional
coverage:
enabled: true
remote: false
Here is the unit.suite.yml
class_name: UnitTester
modules:
enabled:
- Asserts
- Yii1:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
- \Helper\Unit
When I run the tests separately everything works great. e.g.
php codecept.phar run functional --xml --html
php codecept.phar run unit --xml --html
When I run all together
php codecept.phar run --xml --html
It runs the functional without the issue connected with the Yii1 module. And on the unit it brings
[Codeception\Exception\ModuleConfigException]
Yii1 module is not configured!
Couldn't load application config file www/test.php
Please provide application bootstrap file configured for testing
The main goal to run all together is the code coverage.
Here is the answer to my problem:
I created a separate module Yii1Ext(Yii1Ext.php) which extends to Yii1 module:
namespace Helper;
use Codeception\Module\Yii1;
class Yii1Ext extends Yii1
{
public function _initialize()
{
$this->config['appPath'] = FRONTEND.'/'.$this->config['appPath'];
parent::_initialize();
}
}
And in configuration files have the Yii1Ext module enabled instead of Yii1 module
class_name: FunctionalTester
modules:
enabled:
- \Helper\Yii1Ext:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
part: init
- PhpBrowser:
url: 'http://localhost/'
Note: Make sure all defined variables in Yii are defined by the statement
defined('ROOT_DIR') or define('ROOT_DIR', realpath(__DIR__ . '/../'));