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 trying to configure the bundle from : https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
and when I try to run the command :
$ php bin/console doctrine:fixtures:load
getting error:
In LoadDataFixturesDoctrineCommand.php line 95:
Could not find any fixture services to load.
You need to update (app/config/services.yml)
services:
resource: '../../src/DataFixtures/*'
https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
Other problem is here in to the above tutorial
// src/DataFixtures/AppFixtures.php
namespace App\DataFixtures;
name space should be :
namespace DataFixtures;
I want to test my service in Symfony 4 using phpunit bridge, but when i launch the test i get :
Error: Class 'App\Service\CompanyManager' not found
My service is located at src/Service/CompanyManager.php
tests/Service/CompanyManagerTest.php :
namespace App\Tests\Service;
use App\Service\CompanyManager;
use PHPUnit\Framework\TestCase;
use App\Entity\Company;
class CompanyManagerTest extends TestCase
{
public function testGetCompany()
{
$companyManager = new CompanyManager();
$company = $companyManager->getCompany(2);
$this->assertInstanceOf(Company::class,$company);
$company = $companyManager->getCompany(1000);
$this->assertNull($company);
}
}
In config/services_test.yaml, there is this statement :
# If you need to access services in a test, create an alias
# and then fetch that alias from the container. As a convention,
# aliases are prefixed with test. For example:
#
# test.App\Service\MyService: '#App\Service\MyService'
So i tried to add :
test.App\Service\CompanyManager: '#App\Service\CompanyManager'
But i still get the error :
$ ./vendor/bin/simple-phpunit tests
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Testing tests
E 1 / 1
(100%)
Time: 364 ms, Memory: 4.00MB
There was 1 error:
1) App\Tests\Service\CompanyManagerTest::testGetCompany
Error: Class 'App\Service\CompanyManager' not found
C:\...\web\vp20\tests\Service\CompanyManagerTest.php:22
Line 22 is :
$companyManager = new CompanyManager();
Any idea ?
PS : sounds like someone has the same problem there : PHPUnit Error: Class not found
I have just had this problem.
Not sure why, but I didn't have a phpunit.xml.dist in the root of my project.
None of the examples shows this, but if you add bootstrap= and then include the autoloaded. It should start to find your classes.
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
I think you should extend KernelTestCase (Symfony\Bundle\FrameworkBundle\Test\KernelTestCase) instead of TestCase
It is possible that a new class is not yet present in composers list. So... try to run
composer dump-autoload
I'll suggest to use this composer configuration
{
"autoload": {
"psr-4": {
"": ["src", "testS"]
}
}
}
Once you've defined this autoloading configuration in composer.json, and after a composer dump-autoload you should never have issues with autoloading.
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.
Why I'm getting this PHP error?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).
You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('PHPUnit/Framework/TestCase.php');
UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php';
Thanks to Phoenix for pointing this out!
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:
In composer.json:
"phpunit/phpunit": "~4.8|~5.7"
In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.
You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
I submitted a crude PR to start conversation for correcting the documentation.
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Run single test
And then run PHPunit test:
phpunit test.php
Content of test file is following:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
Run test suite
Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
Test suite runs with following commands:
phpunit -c demosuite.xml --testsuite DemoTestSuite
Assumption:
Phpunit (3.7) is available in the console environment.
Action:
Enter the following command in the console:
SHELL> phpunit "{{PATH TO THE FILE}}"
Comments:
You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
I got it working with
include("vendor/autoload.php");
at the top of my test function.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY]
$ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml file in the working directory.
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file