2

When I run phpunit command it throws all info from phpunit help with PHPUnit 3.7.21 by Sebastian Bergmann. So I suggest it should work, but not yet;

when I run

phpunit ExampleTest.php
PHP Fatal error:  Class 'Tests\TestCase' not found in C:\xampp7\htdocs\projects\heroes\tests\Feature\ExampleTest.php on line 8

In Laravel I've got 'tests' folder with

TestCase.php
<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

folder 'Feature' with

ExampleTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

and folder 'Unit'

In Laravel root folder I've got

composer.json




  {
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "laravelcollective/html": "^5.4.0",
        "laravelcollective/html": "~5.0"
    },
    "require-dev": {
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ],
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

I just simply need to start unit testing

4
  • and if you run phpunit without arguments? Commented May 6, 2018 at 8:50
  • as I wrote in the beggining it throws alot of info phpunit PHPUnit 7.1.5 by Sebastian Bergmann and contributors. Usage: phpunit [options] UnitTest [UnitTest.php] phpunit [options] <directory> Code Coverage Options:.... Commented May 6, 2018 at 8:52
  • Please show full content of your composer.json. Commented May 6, 2018 at 8:54
  • I added it higher, also when I run phpunit . it says PHP Fatal error: Class 'Tests\TestCase' not found in ExampleTest.php on line 8 Commented May 6, 2018 at 8:56

4 Answers 4

4

It looks like your using phpunit installed globally in your system, which is super old and it is not connected with your project autoloading. You should use phpunit installed in your project by Composer:

vendor/bin/phpunit
Sign up to request clarification or add additional context in comments.

3 Comments

and you saying that I'm doing some old stuff, please explain what changes I shoul make to become up-to-date
@DimaRashevskiy That depends how do you installed this global version of phpunit. But you should really avoid using global version of phpunit and always use the one installed in project locally. Different projects may require different version of phpunit, so using global installation will make test results unpredictable, since on different computer may be installed different version. Local installation of phpunit will be always the same on every environment, since it is locked in composer.lock.
Thanks. Also for the tests - I'm on basic crud project with crud only. I'm not supposed to cover the crud, but I am supposed to cover the "main logic". So happens that all my methods use DB which is connected to crud, therefore the question IS: what are simple main logic unit tests? I've got one to check the connection. Another test from example chekcs server response from ('/') to get 200, and does not work on ('/mypage'), it gives 500. What could the problem be here and what other simple unit tests can be??
1

When I run

vendor\bin\phpunit

It works, however It would be nice if somebody explained why

Comments

1

You can use this

./vendor/bin/phpunit

like this it will run phpunit from the root of your application

Comments

-1

First install PHP Unit using this comand

composer require --dev phpunit/phpunit

After that check whether it is installed or not

./vendor/bin/phpunit --version

More Details visit - https://technicalguide.net/how-to-use-phpunit-in-laravel/

1 Comment

Welcome to Stack Overflow! When linking to your own site or content (or content that you are affiliated with), you must disclose your affiliation in the answer in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.