简体   繁体   中英

PHP unit - Class 'PHPUnit_Framework_TestCase' not found

I know this question has been asked a hundred times but I am new to phpUnit and I don't know how to use the previous answers to help my case

This is my folder structure

    /src/MyDate.php
    /src/Autoload.php

    /tests/MyDateTest.php

    /vendor/phpunit/phpunit

Autoload files has this in it

<?php

  spl_autoload_register(
    function($class) {
      static $classes = null;
      if ($classes === null) {
        $classes = array(
          'mydate' => '/MyDate.php'
        );
      }
      $cn = strtolower($class);
      if (isset($classes[$cn])) {
        require __DIR__ . $classes[$cn];
      }
    }  
  );

And MyDateTest.php has this

<?php

class MyDateTest extends PHPUnit_Framework_TestCase
{
|
|
test be here

When I run

phpunit --bootstrap= src/autoload.php tests/MyDateTest.php

I get this error

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in C:\wamp64\www\datechallange\tests\MyDateTest.php on line 3

I tried every solution in similar answers. Tried requiring the file with the class but then other classes are missing. I tried modifying the autoload file to include the phpunit library but either I am doing something wrong or that is not the way. I tried to modify the xml file so I don't need to use --bootstrap but I have the same error.

I guess the key is to modify that autoload.php file, but I am not sure how.

Thanks

You appear to have one, or both of two problems.

  • Not using Composer , This will also provide a vendor/autoload.php file, that will automatically pull in any files that have been included via the composer.json file.
  • The latest version of PHPunit (v>6.0) no longer uses the long class names. the still-supported v5+ does though.

The recommended mechanism to install PHPunit 5.7 is:

composer require --dev phpunit/phpunit:^5.7

Alternatively, it can be downloaded as a single phpunit.phar that can be run much as any other PHP script.

You should try this in your test file.

use PHPUnit\Framework\TestCase;

class MyDateTest extends TestCase
{
}

This is the latest correct way of writing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM