2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

単体テストが自動化されていないPHPプロジェクトがあったので改めてPHPUnitを導入してみた

2
Posted at

PHPでの開発プロジェクトで、過去のソースを覗いたところ単体テストが自動化されていませんでした。
いい機会なので導入してみました。

環境

  • WSL2 Ubuntu 24.04

導入手順

1.PHPのバージョンを確認

$ php -v
PHP 8.1.34 (cli) (built: Dec 18 2025 23:36:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.34, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.34, Copyright (c), by Zend Technologies

2.対応するPHPUnitのバージョン確認

Supported Versionsから対応するバージョンを確認。
今回はPHP8.1なので、PHPUnit10が該当しました。

3.PHPUnitの入手と配置

$ wget -O phpunit.phar https://phar.phpunit.de/phpunit-10.phar
$ sudo chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit

4.PHPUnitのバージョン確認

$ phpunit --version

* おおっと *

……実行したところ、怒られました。

PHPUnit requires the "ctype", "dom", "json", "libxml", "mbstring", "tokenizer", "xml", "xmlwriter" extensions, but the "dom", "mbstring", "xml", "xmlwriter" extensions are not available.

PHP拡張モジュールが足りていませんでした。ちゃんと前提条件を確認しましょう

$ sudo apt update
$ sudo apt install -y php8.1-cli php8.1-mbstring php8.1-xml

足りていない拡張モジュールをインストール後、もう一度バージョン確認。

$ phpunit --version
PHPUnit 10.5.63 by Sebastian Bergmann and contributors.

これでphpunitコマンドが実行できるようになりました。

$ phpunit <テストしたいPHP>

あとはテストコードを書いていきます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?