Home  >  Article  >  PHP Framework  >  Introduction to PHPUnit introductory tutorial for Laravel testing (with examples)

Introduction to PHPUnit introductory tutorial for Laravel testing (with examples)

不言
不言forward
2019-03-02 13:55:483082browse

This article brings you an introduction to the PHPUnit introductory tutorial for Laravel testing (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

PHPUnit is one of the oldest and most famous PHP unit testing packages. It is mainly used for unit testing, which means that code can be tested with the smallest possible components, but it is also very flexible and can be used for a lot more than just unit testing.

PHPUnit includes many simple and flexible assertions that allow you to easily test your code, and these assertions are very effective when you are testing specific components. However, it does mean that testing more advanced code such as controllers and form submission validation can be much more complex.

To help make development easier for developers, the Laravel framework includes a series of application testing helpers that allow you to write very simple PHPUnit tests to test complex parts of your application.

The purpose of this tutorial is to introduce you to the basics of PHPUnit testing, using default PHPUnit assertions and the Laravel test helper. The idea is that by the end of this tutorial, you can confidently write basic tests for your application.

Prerequisites

This tutorial assumes that you are already familiar with Laravel and know how to run commands in the application directory (such as the php artisan command). We will create a few basic example classes to learn how different testing tools work, so it is recommended that you create a new application for this tutorial.

If you already have Laravel installed, you can create a new test application by running the following command:

laravel new phpunit-tests

Alternatively, you can create a new application directly using Composer:

composer create-project laravel/laravel --prefer-dist

Other installation methods can also be found in the Laravel documentation.

Create a new test

The first step in using PHPUnit is to create a new test class. The convention for test classes is that they are stored under ./tests/ in the application directory. In this folder, each test class is named <name>Test.php. This format allows PHPUnit to look for each test class --- it will ignore any file that does not end with Test.php.

In your new Laravel application, you will notice that there are two files in the ./tests/ directory: ExampleTest.php and TestCase. php. The TestCase.php file is a bootstrap file used to set up the Laravel environment for our tests. This allows us to use Laravel Facades in our tests and provides a framework for test helpers, which we will cover later. ExampleTest.php is an example test class that contains basic test cases using the Application Test Assistant - ignore it for now.

To create a new test class, we can manually create a new file, or run the Artisan command provided by Laravel make:test

In order to create a new file named BasicTest test class, we only need to run this artisan command:

php artisan make:test BasicTest

Laravel will create a basic test class as shown below:

<?php
class BasicTest extends TestCase
{
    /**
    * 一个基本的测试示例。
    *
    * @return void
    */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

The most important thing to note here The important thing is that the test prefix on the method name is the same as the Test class name suffix, so that the test prefix tells PHPUnit which methods to run when testing. If you forget the test prefix, PHPUnit will ignore the method.

Before we run the test suite for the first time, it is necessary to point out the default phpunit.xml file provided by Laravel. PHPUnit will automatically search for a file named phpunit.xml or phpunit.xml.dist in the current directory when running. Here you can configure specific options for your test.

There is a lot of information in this file, but the most important part for now is the directory definition in testsuite:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit ... >

    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>

    ...
</phpunit>

This will tell the PHPUnit runtime to run in ./ Tests are found in the tests/ directory, which as we learned before is the convention for storing tests.

Now that we have created a basic test and know the PHPUnit configuration, it is time to run the test for the first time.

You can run the test by running the following phpunit command:

./vendor/bin/phpunit

You should see output similar to this:

PHPUnit 4.8.19 by Sebastian Bergmann and contributors.

..

Time: 103 ms, Memory: 12.75Mb

OK (2 tests, 3 assertions)

Now we have With a working PHPUnit setup, it's time to start writing a basic test.

Note that it counts 2 tests and 3 assertions because the ExampleTest.php file contains a test with two assertions. Our new basic test consists of a single assertion, which passes.

Write a basic test

To help with the basic assertions provided by PHPUnit, we will first create a basic class that provides some simple functionality

在 ./app/ 目录中创建一个名为 Box.php 的新文件,并复制此示例类:

<?php
namespace App;

class Box
{
    /**
    * @var array
    */
    protected $items = [];

    /**
    * 使用给定项构造框
    *
    * @param array $items
    */
    public function __construct($items = [])
    {
        $this->items = $items;
    }

    /**
    * 检查指定的项目是否在框中。
    *
    * @param string $item
    * @return bool
    */
    public function has($item)
    {
        return in_array($item, $this->items);
    }

    /**
    * 从框中移除项,如果框为空,则为 null 。
    *
    * @return string
    */
    public function takeOne()
    {
        return array_shift($this->items);
    }

    /**
    * 从包含指定字母开头的框中检索所有项目。
    *
    * @param string $letter
    * @return array
    */
    public function startsWith($letter)
    {
        return array_filter($this->items, function ($item) use ($letter) {
            return stripos($item, $letter) === 0;
        });
    }
}

接下来, 打开你的 ./tests/BasicTest.php 类(我们之前创建的类),并删除默认创建的 testExample 方法, 你应该留一个空类。

我们现在将使用七个基本的 PHPUnit 断言来为我们的 Box 类编写测试。这些断言是:

  • assertTrue()
  • assertFalse()
  • assertEquals()
  • assertNull()
  • assertContains()
  • assertCount()
  • assertEmpty()

assertTrue() 和 assertFalse()

assertTrue() 和 assertFalse() 允许你声明一个值等于 true 或 false 。这意味着它们非常适合测试返回布尔值的方法。在我们的 Box 类中,我们有一个名为 has($item) 的方法,当指定的项在 box 中或不在 box 中时,该方法返回对应返回 true 或 false .

要在 PHPUnit 中为此编写测试,我们可以执行以下操作:

<?php
use App\Box;

class BasicTest extends TestCase
{
    public function testHasItemInBox()
    {
        $box = new Box([&#39;cat&#39;, &#39;toy&#39;, &#39;torch&#39;]);

        $this->assertTrue($box->has('toy'));
        $this->assertFalse($box->has('ball'));
    }
}

注意我们如何只将一个参数传递给 assertTrue() 和 assertFalse() 方法,并且它是 has($item) 方法的输入.

如果您现在运行 ./vendor/bin/phpunit 命令,您会注意到输出包括:

OK (2 tests, 4 assertions)

这意味着我们的测试已经通过。

如果您将 assertFalse() 替换成 assertTrue() 并运行 phpunit 命令,输出将如下所示:

PHPUnit 4.8.19 by Sebastian Bergmann and contributors.

F.

Time: 93 ms, Memory: 13.00Mb

There was 1 failure:

1) BasicTest::testHasItemInBox
Failed asserting that false is true.

./tests/BasicTest.php:12

FAILURES!
Tests: 2, Assertions: 4, Failures: 1.

这告诉我们第12行的断言未能断言 false 值是 true - 因为我们将 assertFalse() 替换为 assertTrue()

将其交换回来,然后重新运行 PHPUnit 。测试应该再次通过,因为我们已经修复了破损的测试。

assertEquals() 与 assertNull()

接下来,让我们看看 assertEquals(), 以及 assertNull()

assertEquals() 用于比较变量实际值与预期值是否相等。我们用它来检查 takeOne() 方法的返回值是否为 Box 内的当前值。当 Box 为空时,takeOne() 将返回 null,我们亦可使用 assertNull() 来进行检查。

与 assertTrue()assertFalse() 以及 assertNull() 不同,assertEquals() 需要两个参数。第一个参数为 预期 值,第二个参数则为 实际 值。

可参照如下代码实现以上断言(assertions):

<?php
use App\Box;

class BasicTest extends TestCase
{
    public function testHasItemInBox()
    {
        $box = new Box([&#39;cat&#39;, &#39;toy&#39;, &#39;torch&#39;]);

        $this->assertTrue($box->has('toy'));
        $this->assertFalse($box->has('ball'));
    }

    public function testTakeOneFromTheBox()
    {
        $box = new Box(['torch']);

        $this->assertEquals('torch', $box->takeOne());

        // 当前 Box 为空,应当为 Null
        $this->assertNull($box->takeOne());
    }
}

运行 phpunit 命令,你应当看到如下输出:

OK (3 tests, 6 assertions)

assertContains() 和 assertCount() 以及 assertEmpty()

终于,我们有三个作用于数组有关的断言,我们能够使用它们去检查 Box 类中的  startsWith($item) 方法。 assertContains() 断言传递进来的数组中包含指定值, assertCount() 断言数组的项数为指定数量,assertEmpty() 断言传递进来的数组为空。

让我们来执行以下测试:

<?php
use App\Box;

class BasicTest extends TestCase
{
    public function testHasItemInBox()
    {
        $box = new Box([&#39;cat&#39;, &#39;toy&#39;, &#39;torch&#39;]);

        $this->assertTrue($box->has('toy'));
        $this->assertFalse($box->has('ball'));
    }

    public function testTakeOneFromTheBox()
    {
        $box = new Box(['torch']);

        $this->assertEquals('torch', $box->takeOne());

        // Null,现在这个 box 是空的。
        $this->assertNull($box->takeOne());
    }

    public function testStartsWithALetter()
    {
        $box = new Box(['toy', 'torch', 'ball', 'cat', 'tissue']);

        $results = $box->startsWith('t');

        $this->assertCount(3, $results);
        $this->assertContains('toy', $results);
        $this->assertContains('torch', $results);
        $this->assertContains('tissue', $results);

        // 如果传递复数断言数组为空
        $this->assertEmpty($box->startsWith('s'));
    }
}

保存并再一次运行你的测试:

OK (4 tests, 9 assertions)

恭喜你,你刚刚使用七个基础的 PHPUnit 断言完成了对 Box 类的全部测试。通过这些简单的断言你能够做许多事,对于其他断言,大多数要更复杂,不过它们仍遵循以上使用规则。

测试你的程序

在你的程序里,对每个组件进行单元测试在很多情况下都是有必要的,而且也应该成为你开发过程中必不可少的一部分,但这并不是你需要做的全部的测试。当你构建一个包含复杂视图、导航和表单的程序时,你同样想测试这些组件。这时,Laravel的测试助手可以使这些测试像单元测试简单组件一样容易。

我们之前查看在 ./tests/ 目录下的默认文件时跳过了 ./tests/ExampleTest.php 文件。 现在打开它,内容如下所示:

<?php
class ExampleTest extends TestCase
{
    /**
* 一个基本功能测试示例。
*
* @return void
*/
    public function testBasicExample()
    {
        $this->visit('/')
             ->see('Laravel 5');
    }
}

我们可以看到这个测试示例非常简单。在不知道测试助手如何运作的情况下,我们可以猜测它的意思如下:

  1. 当我访问/ (根目录)
  2. 我应该看到 'Laravel 5'

如果你打开你的web浏览器,访问我们的程序(如果你没有启动你的web服务器,你可以运行 php artisan serve ),你应该可以在web根目录上看到屏幕上有“Laravel 5”的文本。 鉴于这个测试已经通过了PHPUnit,我们可以很确定地说我们对这个测试示例改造是正确的。

这个测试确保了访问/路径,网页可以返回“'Laravel 5”的文本。一个如此简单的检查也许不代表什么,但如果你的网站上要显示关键信息,它就可以在一个别处的改动导致这个页面无法正常显示正确的信息时,防止你部署一个被损坏的程序。

visit()、see() 以及 dontSee()

现在尝试编写自己的测试,更进一步理解它吧。

首先,编辑 ./app/Http/routes.php ,增加一个新的路由。为了教程目的,我们创建希腊字母定义的路由:

<?php
Route::get(&#39;/&#39;,function () {
    return view(&#39;welcome&#39;);
});

Route::get(&#39;/alpha&#39;,function () {
    return view(&#39;alpha&#39;);
});

然后,创建视图文件 ./resources/views/alpha.blade.php,使用 Alpha 作为关键字,保存基本的HTML文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Alpha</title>
    </head>
    <body>
        <p>This is the Alpha page.</p>
    </body>
</html>

打开浏览器,输入网址: http://localhost:8000/beta,页面会显示出 "This is the Alpha page." 的内容。

现在我们有了测试用到的模版文件,下一步,我们通过运行命令 make:test 来创建一个新的测试文件:

php artisan make:test AlphaTest

然后变成刚创建好的测试文件,按照框架提供的例子,测试 "alpha" 页面上没有包含 "beta" 。 我们可以使用方法 dontSee() ,它是 see() 的对应的反向方法。

下面代码是上面实现的简单例子:

<?php
class AlphaTest extends TestCase
{
    public function testDisplaysAlpha()
    {
        $this->visit('/alpha')
             ->see('Alpha')
             ->dontSee('Beta');
    }
}

保存并运行 PHPUnit (./vendor/bin/phpunit),测试代码应该会全部通过,你会看到像这样的测试状态内容显示:

OK (5 tests,12 assertions)

开发前先写测试

对于测试来说,测试驱动开发 (TDD) 是非常酷的方法,首先我们先写测试。写完测试并执行它们,你会发现测试没通过,接下来 我们编写满足测试的代码,再次执行测试,使测试通过。 接下来让我们开始。

首先,建立一个 BetaTest 类使用 make:test artisan 命令:

php artisan make:test BetaTest

接下来,更新测试用例以便检查 /beta 的路由 route 为「Beta」:

<?php
class BetaTest extends TestCase
{
    public function testDisplaysBeta()
    {
        $this->visit('/beta')
             ->see('Beta')
             ->dontSee('Alpha');
    }
}

现在使用 ./vendor/bin/phpunit 命令来执行测试。结果是一个看起来简洁但不好的错误信息,如下:

> ./vendor/bin/phpunit
PHPUnit 4.8.19 by Sebastian Bergmann and contributors.

....F.

Time: 144 ms, Memory: 14.25Mb

There was 1 failure:

1) BetaTest::testDisplaysBeta
一个对 [http://localhost/beta] 的请求失败了。收到状态码 [404]。

...

FAILURES!
Tests: 6, Assertions: 13, Failures: 1.

我们现在需要创建这个不存在的路由。让我们开始。

首先,编辑 ./app/Http/routes.php 文件来创建新的 /beta 路由:

<?php
Route::get(&#39;/&#39;, function () {
    return view(&#39;welcome&#39;);
});

Route::get(&#39;/alpha&#39;, function () {
    return view(&#39;alpha&#39;);
});

Route::get(&#39;/beta&#39;, function () {
    return view(&#39;beta&#39;);
});

接下来,在 ./resources/views/beta.blade.php 下创建如下视图模版:

<!DOCTYPE html>
<html>
    <head>
        <title>Beta</title>
    </head>
    <body>
        <p>This is the Beta page.</p>
    </body>
</html>

现在再一次执行 PHPUnit,结果应该再一次回到绿色。

> ./vendor/bin/phpunit
PHPUnit 4.8.19 by Sebastian Bergmann and contributors.

......

Time: 142 ms, Memory: 14.00Mb

OK (6 tests, 15 assertions)

这样我们就通过在完成新的页面之前写测试的方式,对 测试驱动开发 进行了实践。

click() 和 seePageIs()

Laravel 也提供一个辅助函数 (click()) 允许测试点击页面中存在的连接 ,以及一个方法 (seePageIs()) 检查点击展示的结果页面。

让我们使用这两个辅助函数去执行在 Alpha 和 Beta 页面的链接。

首先,我们更新我们的测试。打开 AlphaTest 类,我们将添加一个新的测试方法,这将点击 「alpha」页面上的「Next」链接跳转到 「beta」页面。

新的测试代码如下:

<?php
class AlphaTest extends TestCase
{
    public function testDisplaysAlpha()
    {
        $this->visit('/alpha')
             ->see('Alpha')
             ->dontSee('Beta');
    }

    public function testClickNextForBeta()
    {
        $this->visit('/alpha')
             ->click('Next')
             ->seePageIs('/beta');
    }
}

注意到,在我们新建的 testClickNextForBeta() 方法中,我们并没有检查每一个页面的内容。 其他测试都成功的检查了两个页面的内容,所以这里我们只关心点击 「Next」链接将发送到 /beta

你现在可以运行测试组件了,但就像预料的一样测试将不通过,因为我们还没有更新我们的 HTML。

接下来,我们将更新 BetaTest 来做类似的事情:

<?php
class BetaTest extends TestCase
{
    public function testDisplaysBeta()
    {
        $this->visit('/beta')
             ->see('Beta')
             ->dontSee('Alpha');
    }

    public function testClickNextForAlpha()
    {
        $this->visit('/beta')
             ->click('Previous')
             ->seePageIs('/alpha');
    }
}

接下来,我们更新我们的 HTML 模版。

./resources/views/alpha.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Alpha</title>
    </head>
    <body>
        <p>This is the Alpha page.</p>
        <p><a href="/beta">Next</a></p>
    </body>
</html>

./resources/views/beta.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Beta</title>
    </head>
    <body>
        <p>This is the Beta page.</p>
        <p><a href="/alpha">Previous</a></p>
    </body>
</html>

保存文件,再一次执行 PHPUnit:

> ./vendor/bin/phpunit
PHPUnit 4.8.19 by Sebastian Bergmann and contributors.

F....F..

Time: 175 ms, Memory: 14.00Mb

There were 2 failures:

1) AlphaTest::testDisplaysAlpha
Failed asserting that '<!DOCTYPE html>
<html>
    <head>
        <title>Alpha</title>
    </head>
    <body>
        <p>This is the Alpha page.</p>
        <p><a href="/beta">Next</a></p>
    </body>
</html>
' does not match PCRE pattern "/Beta/i".

2) BetaTest::testDisplaysBeta
Failed asserting that '<!DOCTYPE html>
<html>
    <head>
        <title>Beta</title>
    </head>
    <body>
        <p>This is the Beta page.</p>
        <p><a href="/alpha">Previous</a></p>
    </body>
</html>
' does not match PCRE pattern "/Alpha/i".

FAILURES!
Tests: 8, Assertions: 23, Failures: 2.

然而测试失败了。如果你仔细观察我们的新 HTML,你将注意到我们分别有术语 beta 和 alpha 在 /alpha 和 /beta 页面。这意味着我们需要稍微更改我们的测试让它们与误报不匹配。

在每一个 AlphaTest 和 BetaTest 类,更新 testDisplays* 方法去使用 dontSee('<page> page')。通过这种方式,这将仅仅匹配字符串而不是那个术语。

两个测试文件如下所示:

./tests/AlphaTest.php

<?php
class AlphaTest extends TestCase
{
    public function testDisplaysAlpha()
    {
        $this->visit('/alpha')
             ->see('Alpha')
             ->dontSee('Beta page');
    }

    public function testClickNextForBeta()
    {
        $this->visit('/alpha')
             ->click('Next')
             ->seePageIs('/beta');
    }
}

./tests/BetaTest.php

<?php
class BetaTest extends TestCase
{
    public function testDisplaysBeta()
    {
        $this->visit('/beta')
             ->see('Beta')
             ->dontSee('Alpha page');
    }

    public function testClickNextForAlpha()
    {
        $this->visit('/beta')
             ->click('Previous')
             ->seePageIs('/alpha');
    }
}

再一次运行你的测试,所有的测试都应该通过了。我们现在已经测试我们所有的新文件,包括页面中的 Next/Previous 链接。

通过 Semaphore 对 PHPUnit 持续集成

通过 Semaphore设置 持续集成你可以自动执行你的测试。

这样每一次你进行 git push 提交代码的时候都会执行你的测试,并且 Semaphore 预装了所有最新的  PHP 版本。

如果你还没有一个 Semaphore 账户, 先去 注册一个免费的 Semaphore 账户 。接下来需要做的是将它 添加到你的项目,并按照提示逐步去做来执行你的测试:

composer install --prefer-source
phpunit

关于 PHP 持续集成 的更多信息,请参照 Semaphore 文档。

结语

你应该注意到本教程中的所有测试都有一个共同的主题:它们都非常简单。 这是学习如何使用基本的测试断言和辅助函数,并且尽可能的使用它们的好处之一。编写测试越简单,测试就越容易理解和维护。

掌握了本教程中介绍的 PHPUnit 断言之后,你还可以去 PHPUnit 文档 找到更多内容。 所有的断言都遵循基本的模式,但你会发现,在大多数测试中都会返回基本的断言。

对于 PHPUnit 断言来说,Laravel 的测试辅助函数是极好的补充,这让应用程序的测试变的非常容易。也就是说,重要的是要认识到,对于我们写测试,我们只检查关键信息,而不是整个页面。这使得测试变得简单,并允许页面内容随着应用程序的变化而变化。如果关键信息仍然存在,测试仍然通过,每个人都会满意。


The above is the detailed content of Introduction to PHPUnit introductory tutorial for Laravel testing (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete