phpunit入门
This post is outdated – for a more up-to-date introduction, please see the recently published re-introduction!
这篇文章已经过时了-有关最新的介绍,请参阅最近发布的重新介绍 !
Most people know that testing your websites is a good idea, but after some time testing can become tedious. What if a lot of this testing process could be automated so you don’t have to go trough every function manually, time after time, to ensure that it still works after updating your code? This is where unit testing comes in, to automate the testing process.
大多数人都知道测试您的网站是一个好主意,但是一段时间后测试可能会变得乏味。 如果很多这样的测试过程可以自动化进行,那么您不必一次又一次地遍历每个功能,以确保在更新代码后仍能正常工作该怎么办? 这是进行单元测试的地方,可以使测试过程自动化。
Unit testing makes it easier, and above all safer, to modify your code because it catches any irregularities in the behavior (i.e. bugs) that may be introduced with the new code. In this article you will learn the absolute basics of unit testing with PHPUnit and how easy it is to get started using it as I guide you trough the process of writing your first test.
单元测试使修改代码变得更加容易,最重要的是更加安全,因为它可以捕获新代码可能引入的行为异常(即错误)。 在本文中,您将学习使用PHPUnit进行单元测试的绝对基础知识,以及在我指导您编写第一个测试过程的过程中开始使用它有多么容易。
Before you can start writing your first unit test, you need to have PHPUnit installed. It can easily be installed using the PEAR installer, and the process is documented in PHPUnit’s online manual at www.phpunit.de/manual/current/en/installation.html.
在开始编写第一个单元测试之前,需要安装PHPUnit。 可以使用PEAR安装程序轻松安装它,该过程在PHPUnit的在线手册中进行了记录,网址为www.phpunit.de/manual/current/en/installation.html 。
Now it’s time to get your hands dirty writing your first test! To get started, you need something to test, so for the first example I’ve written a very simple PHP class representing a user:
现在是时候动手编写您的第一个测试! 首先,您需要测试一些东西,因此对于第一个示例,我编写了一个非常简单的表示用户PHP类:
<?php
class User {
protected $name;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function talk() {
return "Hello world!";
}
}
Let’s say you want to ensure that the user always says hello; it would be devastating for example if she started meowing like a cat all of a sudden!
假设您要确保用户始终打招呼; 例如,如果她突然突然像猫一样喵喵叫,那将是灾难性的!
For this you need to make a new test class, which I’ve arbitrarily named UserTest
. It doesn’t matter what you call your test classes but it’s generally a good idea to name them after the classes you’re testing.
为此,您需要创建一个新的测试类,我将其随意命名为UserTest
。 所谓的测试类无关紧要,但是通常以您要测试的类来命名它们是一个好主意。
To create the test class, you need to include the class you’re testing as well as PHPUnit’s autoloading functionality. Then you define the test class which extends PHPUnit_Framework_TestCase
.
要创建测试类,您需要包括要测试的类以及PHPUnit的自动加载功能。 然后,定义扩展PHPUnit_Framework_TestCase
的测试类。
<?php
require_once "PHPUnit/Autoload.php";
require_once "User.php";
class UserTest extends PHPUnit_Framework_TestCase
{
}
This is the class in which you will write your tests, and every test will have its own method.
这是编写测试的类,每个测试都有自己的方法。
The assertEquals()
method defined in PHPUnit_Framework_TestCase
does just what you would assume, it asserts whether something is equal or not. Since UserTest
is a subclass of PHPUnit_Framework_TestCase
, you can use it with $this
.
PHPUnit_Framework_TestCase
中定义的assertEquals()
方法可以实现您所假定的功能,它断言某项是否相等。 由于UserTest
是PHPUnit_Framework_TestCase
的子类,因此可以将其与$this
一起使用。
To ensure the user says an appropriate greeting, write the following method:
为确保用户说出适当的问候语,请编写以下方法:
<?php
...
class UserTest extends PHPUnit_Framework_TestCase
{
// test the talk method
public function testTalk() {
// make an instance of the user
$user = new User();
// use assertEquals to ensure the greeting is what you
$expected = "Hello world!";
$actual = $user->talk();
$this->assertEquals($expected, $actual);
}
}
Needing to setup a new user in every test method can become quite tedious. This is where PHPUnit’s fixtures can help. A fixture is when you setup a certain state and after every test the state is reset back to the way it was. How does this work?
在每种测试方法中都需要设置新用户会变得很繁琐。 这就是PHPUnit的固定装置可以提供帮助的地方。 固定装置是指您设置某种状态,并且在每次测试后将状态重置为原来的状态。 这是如何运作的?
Let’s say you have your object $user
and you override the inherited setUp()
method:
假设您有对象$user
并且重写了继承的setUp()
方法:
<?php
...
class UserTest extends PHPUnit_Framework_TestCase
{
protected $user;
...
protected function setUp() {
$this->user = new User();
$this->user->setName("Tom");
}
}
Here you’ve instantiated your user and set his name to Tom.
在这里,您已实例化您的用户并将其名称设置为Tom。
When you are done with all the tests you might want to unset the user; for this you can override the tearDown()
method:
完成所有测试后,您可能希望取消设置用户。 为此,您可以重写tearDown()
方法:
<?php
...
class UserTest extends PHPUnit_Framework_TestCase
{
...
protected function tearDown() {
unset($this->user);
}
...
}
The setUp()
and tearDown()
methods are called by PHPUnit before and after each test, so you can skip instantiating the user in the test method. testTalk()
now becomes:
在每次测试之前和之后,PHPUnit都会调用setUp()
和tearDown()
方法,因此您可以跳过在测试方法中实例化用户的步骤。 testTalk()
现在变为:
<?php
...
class UserTest extends PHPUnit_Framework_TestCase
{
...
public function testTalk() {
$expected = "Hello world!";
$actual = $this->user->talk();
$this->assertEquals($expected, $actual);
}
}
Now that you have a test class that defines all your tests, wouldn’t it be nice to run them? If you have successfully installed everything, you should be able to simply run the tests from a terminal.
既然您已经有了定义所有测试的测试类,那么运行它们不是很好吗? 如果成功安装了所有内容,则应该可以从终端简单运行测试。
michelle@testbed:~$ phpunit UnitTest UserTest.php
PHPUnit 3.6.3 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 5.75Mb
OK (1 test, 1 assertion)
Congratulations! You have now written and run your first unit test with PHPUnit!
恭喜你! 现在,您已经编写并使用PHPUnit运行您的第一个单元测试!
Do you see the little dot there? For every test run there will be a character indicating the result. The characters are as follows:
你看到那里的小点了吗? 对于每次测试,都会有一个字符指示结果。 字符如下:
Right now the two most important ones you have to worry about are the dot and the F, as these indicate whether your test was a success or failure.
现在,您需要担心的两个最重要的问题是点和F,因为这表明您的测试是成功还是失败。
So, what happens when a test fails? Let’s change the User
so that they actually say something completely unexpected, like “blubb”.
那么,测试失败会怎样? 让我们更改User
使他们实际上说出完全出乎意料的话,例如“ blubb”。
<?php
class User {
...
public function talk() {
return "blubb";
}
}
Now run the test as you did before.
现在像以前一样运行测试。
michelle@testbed:~$ phpunit UnitTest UserTest.php
PHPUnit 3.6.3 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) UserTest::testTalk
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-Hello World!
+blubb
/PHPUnit introduction/tests/UserTest.php:23
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
As you can see there’s the F symbol to indicate failure. There’s also more information about the failure where you can see that it failed asserting the two strings were equal; it expected “Hello World!” but got “blubb”.
如您所见,有F符号指示失败。 还有更多关于失败的信息,您可以看到失败表明两个字符串相等。 它期望“ Hello World!” 但是得到了“ blubb”。
In this article you discovered that it isn’t so difficult to get started with unit-testing. The basics are very simple. However, there is a lot more to unit-testing than meets the eye. This simple test situation should be plenty to get you started writing your own tests, but try expanding it, getting errors and failing the test. Try expanding the User
class and writing additional tests for it. How about adding a height
, hair_color
, or birthday
property?
在本文中,您发现开始进行单元测试并不那么困难。 基础很简单。 但是,单元测试要比实际需要的多得多。 这种简单的测试情况应该足以让您开始编写自己的测试,但是请尝试对其进行扩展,出错并导致测试失败。 尝试扩展User
类并为其编写其他测试。 如何增加一个height
, hair_color
,或birthday
的财产?
A few useful tips I can offer that will help you along the way are:
我可以提供一些有用的技巧,以帮助您:
Try calling phpunit --help
in the terminal to see what you can do with PHPUnit.
尝试在终端中调用phpunit --help
,以了解您可以使用PHPUnit做什么。
If you’re wondering how to test something specific, the PHPUnit manual is actually very good. I briefly mentioned fixtures, and there’s a complete description of them in the manual.
如果您想知道如何测试特定的东西,PHPUnit手册实际上非常好。 我简要提到了灯具,并且在手册中有完整的描述 。
The manual section covering assertions really rocks when you’re just starting out. Make sure to check it out.
当您刚开始时,涉及断言的手册部分确实很困难。 一定要检查一下。
If you want to download the test code from this article to experiment with, it’s available at GitHub to do as you please.
如果您想从本文中下载测试代码以进行试验, 可以在GitHub上随意执行。
I hope this tutorial helped you to get started with PHPUnit. Try it out, learn from your mistakes, don’t be afraid to ask any questions, and above all have fun!
我希望本教程可以帮助您开始使用PHPUnit。 尝试一下,从错误中学习,不要害怕问任何问题,最重要的是要开心!
Editor Note 19 Oct 2012: The accompanying code available on GitHub has been updated to use Composer to download the PHPUnit dependency. The require statement has been changed to call vendor/autoload.php to procure an autoloader. Running the test can be done by navigating into the tests directory and invoking ../vendor/bin/phpunit UnitTest UserTest.php.
编辑说明2012年10月19日: GitHub上的随附代码已更新,可以使用Composer下载PHPUnit依赖项。 require语句已更改为调用vendor / autoload.php来获取自动加载器。 可以通过导航到测试目录并调用../vendor/bin/phpunit UnitTest UserTest.php来运行测试。
Image via Archipoch / Shutterstock
翻译自: https://www.sitepoint.com/getting-started-with-phpunit/
phpunit入门