Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active November 24, 2017 00:30
Show Gist options
  • Save danilobatistaqueiroz/9921bc8f53fd4256f23ef8943cd35c9b to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/9921bc8f53fd4256f23ef8943cd35c9b to your computer and use it in GitHub Desktop.
configuring phpunit

Configuring phpunit

to use phpunit you can download the phpunit.phar:

phpunit.phar file

After downloading, copy phpunit.phar file into a folder

(you can download in a folder that is mapped on %PATH% environment variable, or in the project folder)

An example of use:

php.exe phpunit.phar tests\myTest.php


but a better approach is to use composer:

there are some ways to install using composer.

its possible to initialize a new project, use the command:

composer require --dev phpunit\phpunit

this command will download the latest stable version inside vendor folder.

if you are in an existent composer project, then add phpunit dependecy to composer.json:

    "require-dev": {
        "phpunit/phpunit": "^6.4",
        ...

and execute:

composer update


Executing the first test

create a file for tests named UserTest.php:

<?php

use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    public function testTalkToTheWorld() {
        $expected = "Hello world!";
        $actual = 'Hello !';
        $this->assertEquals($expected, $actual);
    }
}

Execute in the root folder:

vendor/bin/phpunit --color --testdox UserTest.php

testdox option: Report test execution progress in TestDox format.

or simply:

vendor/bin/phpunit UserTest.php


links:

https://phpunit.de/manual/current/en/

https://jonathanmh.com/unit-testing-phpunit-windows/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment