PHPUnit是一个功能强大且通用的PHP单元测试框架,可帮助开发人员以简单有效的方式为其代码编写和运行测试。它提供了一系列功能和工具,使测试PHP代码更容易,更快捷,并有助于确保代码按预期运行。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上安装 PHPUnit
第 1 步。首先,通过在终端中运行以下命令,确保所有系统软件包都是最新的。apt
<span class="pln">sudo apt update sudo apt upgrade sudo apt install wget apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https gnupg2 software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common</span>
第 2 步。安装 PHP。
在我们开始之前,请确保您在 Ubuntu 系统上安装了必要的 PHP。如果您没有安装 PHP,您可以按照我们的指南进行操作。
第 3 步。在 Ubuntu 22.04 上安装 PHPUnit。
默认情况下,PHPUnit 在 Ubuntu 22.04 基本存储库中可用。现在运行以下命令,将最新版本的 PHPUnit 安装到您的 Ubuntu 系统中:
<span class="pln">sudo apt install phpunit</span>
若要验证是否已正确安装 PHPUnit,请运行以下命令:
<span class="pln">phpunit </span><span class="pun">--</span><span class="pln">version</span>
您应该会看到已安装的 PHPUnit 的版本号以及其他一些信息。
第 4 步。使用 PHPUnit。
一旦安装了 PHPUnit,您就可以开始使用它来编写和运行 PHP 代码的测试。要创建新测试,请创建一个新的 PHP 文件并包含以下代码:
<code class="!whitespace-pre hljs language-php prettyprinted"><span class="hljs-meta"><span class="pun"><?</span><span class="pln">php</span></span><span class="hljs-keyword"><span class="pln">declare</span></span><span class="pun">(</span><span class="pln">strict_types</span><span class="pun">=</span><span class="hljs-number"><span class="lit">1</span></span><span class="pun">);</span><span class="hljs-keyword"><span class="kwd">use</span></span> <span class="hljs-title"><span class="typ">PHPUnit</span></span><span class="pln">\</span><span class="hljs-title"><span class="pln">Framework</span></span><span class="pln">\</span><span class="hljs-title"><span class="pln">TestCase</span></span><span class="pun">;</span><span class="hljs-keyword"><span class="kwd">final</span></span> <span class="hljs-class"><span class="hljs-keyword"><span class="kwd">class</span></span> <span class="hljs-title"><span class="typ">ExampleTest</span></span> <span class="hljs-keyword"><span class="kwd">extends</span></span> <span class="hljs-title"><span class="typ">TestCase</span></span></span><span class="pun">{</span> <span class="hljs-keyword"><span class="kwd">public</span></span> <span class="hljs-function"><span class="hljs-keyword"><span class="kwd">function</span></span> <span class="hljs-title"><span class="pln">testAddingTwoNumbers</span></span><span class="pun">():</span> <span class="hljs-title"><span class="kwd">void</span></span></span><span class="pun">{</span> <span class="hljs-variable"><span class="pln">$result</span></span> <span class="pun">=</span> <span class="hljs-number"><span class="lit">2</span></span> <span class="pun">+</span> <span class="hljs-number"><span class="lit">2</span></span><span class="pun">;</span> <span class="hljs-variable language_"><span class="pln">$this</span></span><span class="pun">-></span><span class="hljs-title function_ invoke__"><span class="pln">assertEquals</span></span><span class="pun">(</span><span class="hljs-number"><span class="lit">4</span></span><span class="pun">,</span> <span class="hljs-variable"><span class="pln">$result</span></span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span></code>
将此文件另存为 “” 并运行以下命令:example-test.php
<span class="pln">phpunit example</span><span class="pun">-</span><span class="pln">test</span><span class="pun">.</span><span class="pln">php</span>
感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上安装 PHPUnit。如需其他帮助或有用信息,我们建议您查看 PHPUnit 官方网站。