Help us understand the problem. What is going on with this article?

PHPUnit とりあえず動かすとこまで

More than 1 year has passed since last update.

phpunitのインストール

  • composer.json
composer.json
{
    "require-dev": {
        "phpunit/phpunit": "*"
    }
}
  • インストール
$./composer.phar install

ディレクトリの作成

$ mkdir src
$ mkdir tests

テスト対象ソースをautoloadするように設定する

composer.json
{
    "require-dev": {
        "phpunit/phpunit": "*"
    },

    "autoload": {
        "psr-4": {
            "Sample\\": "src/"
        }
    }

}

実装

  • テストコード
tests/SampleTest.php
<?php

require_once('vendor/autoload.php');

class SampleTest extends PHPUnit\Framework\TestCase {

    public function test_add() {
        $sample = new Sample\Sample();
        $this->assertEquals(10, $sample->Add(4, 6));
    }

    public function test_sub() {
        $sample = new Sample\Sample();
        $this->assertEquals(1, $sample->Sub(7, 6));
    }

}
  • テスト対象コード
src/Sample.php
<?php

namespace Sample;

class Sample {

    // $a + $b を返す                                                                                                                                                                                                                                                           
    public function add($a, $b) {
        return $a + $b;
    }

    // $a - $b を返す                                                                                                                                                                                                                                                           
    public function sub($a, $b) {
        return $a - $b;
    }

}

テスト実行

$ vendor/bin/phpunit tests/
PHPUnit 7.1.4 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 21 ms, Memory: 4.00MB

OK (2 tests, 2 assertions)
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away