質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
PHPUnit

PHPUnitは、PHP向けのユニット・テスト向けフレームワークで、手動では手間のかかるテスト作業を自動化し、繰り返し実行することが可能です。

Q&A

解決済

1回答

1802閲覧

PHPUnitテストがうまくいかない

退会済みユーザー

退会済みユーザー

総合スコア0

PHPUnit

PHPUnitは、PHP向けのユニット・テスト向けフレームワークで、手動では手間のかかるテスト作業を自動化し、繰り返し実行することが可能です。

0グッド

0クリップ

投稿2020/06/14 06:15

編集2020/07/04 12:50

参考書でphpを勉強しています。PHPUnitテストがうまくいかないので質問します。
restaurant_check()関数は、料理の値段、税率、チップ率を指定すると、レストランの
料理の総額を計算します。
RestaurantCheckTest()関数は、restaurant_check()関数をテストする関数です。
約1ケ月ぐらいPHPUnitをネットで調べて初歩のテストができるようになったので、
参考書に戻ってやってみたのですが、
どうしても以下のエラーが解決できません。

XAMPP for Windows 7.4.7

php -v

PHP 7.4.7 (cli) (built: May 12 2020 11:38:54) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

httpd -v

Server version: Apache/2.4.43 (Win64)
Apache Lounge VC15 Server built: Apr 22 2020 11:11:00

phpunit -v

PHPUnit 9.2.5 by Sebastian Bergmann and contributors.

ファイルは以下のようになっています。
C:\MyPhpUnit\phpunit_chap13\srcrestaurant_check.php

C:\MyPhpUnit\phpunit_chap13\test\RestaurantCheckTest.php
C:\MyPhpUnit\phpunit_chap13\phpunit.phar
C:\MyPhpUnit\phpunit_chap13\phpunit.cmd

よろしくお願いいたします。

php

1//restaurant_check.php 2<?php 3 4 // 引数値の変更 5 function restaurant_check($meal,$tax,$tip){ 6 7 $tax_amount = $meal*($tax/100); 8 9 $tip_amount = $meal*($tip/100); 10 11 $total_amount = $meal + $tax_amount + $tip_amount ; 12 13 return $total_amount; 14 } 15?>

php

1 2//RestaurantCheckTest.php 3include 'restaurant_check.php'; 4 5 // 引数値の変更 6 class RestaurantCheckTest extends PHPUnit_Framework_TestCase{ 7 8 public function testWithTaxAndTip() { 9 10 $meal =100; 11 12 $tax = 10; 13 14 $tip = 20 ; 15 16 $result = resturant_check($meal,$tax,$tip); 17 18 $this->assertEquals(130,$result); 19 20 } 21 22 }
// phpunit.cmd @php "%~dp0phpunit.phar" %*
hiroko@HIROKO711 C:\MyPhpUnit\phpunit_chap13 # phpunit test\RestaurantCheckTest.php PHPUnit 9.2.5 by Sebastian Bergmann and contributors. E Time: 00:00.001, Memory: 12.00 MB There was 1 error: 1) RestaurantCheckTest::testWithTaxAndTip Error: Call to undefined function resturant_check() C:\MyPhpUnit\phpunit_chap13\test\RestaurantCheckTest.php:15 ERRORS! Tests: 1, Assertions: 0, Errors: 1.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

1.RestaurantCheckTest.phpの1行目include 'restaurant_check.php';を

include './src/restaurant_check.php';に修正する

2.// 引数値の変更
class RestaurantCheckTest extends PHPUnit_Framework_TestCase{を

class RestaurantCheckTest extends PHPUnit\Framework\TestCase{に修正する。

3.RestaurantCheckTest.phpの最後から2行目$result = resturant_check($meal,$tax,$tip);を
$result = restaurant_check($meal,$tax,$tip);

4.phpunit.cmdを追加する

5.https://phar.phpunit.de/phpunit.pharのリンクをクリックしphpunit-9.2.5 .pharを

ダウンロードして,C:\MyPhpUnit\phpunit_chap13\に張り付けて名前をphpunit.pharに変更する。
C:\MyPhpUnit\phpunit_chap13\phpunit.phar

投稿2020/07/04 12:41

編集2020/07/04 13:13
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問