日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

使用Atoum测试PHP代码-PHPUnit的替代方法

發(fā)布時(shí)間:2023/12/31 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Atoum测试PHP代码-PHPUnit的替代方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

This article was peer reviewed by Christopher Pitt, @jubianchi, and Ivan Enderlin. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

這篇文章由Christopher Pitt , @ jubianchi和Ivan Enderlin進(jìn)行了同行評(píng)審。 感謝所有SitePoint的同行評(píng)審人員使SitePoint內(nèi)容達(dá)到最佳狀態(tài)!

If you’ve been around PHP for more than a little while, you’ve no doubt started to test your code. And if you ask anyone in the PHP space what to use for writing unit tests, likely the first answer that they’ll give you is PHPUnit.

如果您使用PHP已經(jīng)有一段時(shí)間了,那么您無疑已經(jīng)開始測試您的代碼。 而且,如果您問PHP領(lǐng)域的任何人用來編寫單元測試的內(nèi)容,那么他??們給您的第一個(gè)答案可能就是PHPUnit。

It’s the de facto standard in the PHP community, and with good reason. But it’s not the only choice. Whilst it does command the lion’s share, other choices abound, one of which I’m going to take you through in this tutorial; it’s called atoum.

這是PHP社區(qū)中的事實(shí)上的標(biāo)準(zhǔn),并且有充分的理由。 但這不是唯一的選擇。 雖然它確實(shí)占據(jù)了很大的份額,但還有很多其他選擇,在本教程中,我將帶您進(jìn)行選擇。 它叫做atoum 。

If this is your first time hearing about it, it is self-described as:

如果這是您第一次聽說,則自我描述為:

A simple, modern, and intuitive unit testing framework for PHP!

一個(gè)簡單,現(xiàn)代,直觀PHP單元測試框架!

I can’t necessarily vouch for it being all that intuitive, but it definitely is reasonably simple to use. And whilst its composer.json currently specifies a minimum version of PHP 5.3.3, one of the core developers told me that from version 3.0 support for PHP 5.3 will be officially dropped in favor of PHP 5.6. So don’t be fooled, it is a modern take on testing in PHP.

我不一定要保證它這么直觀,但是使用起來肯定很簡單。 盡管其composer.json當(dāng)前指定了PHP 5.3.3的最低版本,但一位核心開發(fā)人員告訴我,從3.0版起,對(duì)PHP 5.3的支持將正式放棄,而支持PHP 5.6。 因此,不要被愚弄了,這是用PHP進(jìn)行測試的現(xiàn)代方式。

Recently, I decided to give it a test run on an existing codebase of mine, a health monitoring application which I’ve been writing to help me deal with some health problems I’ve had over the last 12 – 24 months. I found it to be an interesting take on software testing, combining several different styles into the one package.

最近,我決定在現(xiàn)有的我的代碼庫上進(jìn)行測試,這是一個(gè)健康監(jiān)控應(yīng)用程序,我一直在編寫該程序以幫助我解決過去12到24個(gè)月中遇到的一些健康問題。 我發(fā)現(xiàn)這是對(duì)軟件測試的一種有趣嘗試,它將幾種不同的樣式組合到一個(gè)軟件包中。

安裝Atoum (Installing Atoum)

Like all modern PHP software, we install it with Composer.

像所有現(xiàn)代PHP軟件一樣,我們使用Composer進(jìn)行安裝。

composer require atoum/atoum

For the purposes of this tutorial, I’ll assume that you’re following along using PhpStorm. No offense meant to Vim, Emacs, and other editors.

出于本教程的目的,我假設(shè)您正在使用PhpStorm。 對(duì)Vim , Emacs和其他編輯者而言,并不構(gòu)成冒犯。

Given that, and given how atoum’s source code is structured, we should also install a supplementary library called atoum/stubs.

鑒于此,并考慮了atoum的源代碼的結(jié)構(gòu),我們還應(yīng)該安裝一個(gè)稱為atoum / stubs的補(bǔ)充庫。

Without it, attempting to perform any kind of code completion in your IDE with atoum is less than straight-forward. Stubs make working with atoum much more user-friendly, as the graphic below will attest.

如果沒有它,嘗試使用atoum在IDE中執(zhí)行任何類型的代碼完成都不會(huì)很簡單。 存根使使用Atoum更加用戶友好,如下圖所示。

composer require atoum/stubs

配置Atoum (Configuring Atoum)

Now that it’s installed, let’s perform some preliminary configuration. Specifically, what we’re going to do is to configure how code coverage will reported, as well as what the test reporting will look like when run from the terminal.

現(xiàn)在已經(jīng)安裝了,讓我們執(zhí)行一些初步配置。 具體來說,我們要做的是配置如何報(bào)告代碼覆蓋率以及從終端運(yùn)行時(shí)測試報(bào)告的外觀。

This isn’t strictly necessary, but as we may be looking at the test output in the terminal a lot, it helps to give a bit of life to it. Second, whilst the built-in code coverage is pretty good, sometimes viewing it through a browser is just easier to do, as we’ll see in a little while.

這不是嚴(yán)格必要的,但是由于我們可能會(huì)經(jīng)常查看終端中的測試輸出,因此有助于延長使用壽命。 其次,盡管內(nèi)置代碼覆蓋范圍非常好,但是有時(shí)通過瀏覽器查看它只是更容易完成,我們將在稍后看到。

If this is your first time hearing about code coverage, code coverage reports the percentage of the codebase which is covered by tests. Code coverage reports help us know how complete our tests are, and how confident we can feel when making changes to the code. So, while there is some work involved in getting code coverage set up, it’s worth having in place.

如果這是您第一次了解代碼覆蓋率,則代碼覆蓋率將報(bào)告測試覆蓋的代碼庫百分比。 代碼覆蓋率報(bào)告可幫助我們了解測試的完成程度以及對(duì)代碼進(jìn)行更改時(shí)的信心。 因此,盡管需要進(jìn)行一些工作來設(shè)置代碼覆蓋率,但是值得一試。

To begin configuring atoum, create a new file in the root directory of your project, called .atoum.php. There, add the following contents:

要開始配置Atoum,請(qǐng)?jiān)陧?xiàng)目的根目錄中創(chuàng)建一個(gè)名為.atoum.php的新文件。 在那里,添加以下內(nèi)容:

<?phpuse mageekguy\atoum;$stdout = new atoum\writers\std\out; $report = new atoum\reports\realtime\santa; $script->addReport($report->addWriter($stdout) );

No, you’re not dreaming that the reporting style is set to Santa. If you’re groaning that it’s still a few months until Christmas, it is already October. I’m sure that the shops in your area will soon be filled with all the tacky Christmas merchandise.

不,您不是在夢(mèng)想將報(bào)告樣式設(shè)置為圣誕老人。 如果您抱怨到圣誕節(jié)還有幾個(gè)月,那已經(jīng)是十月了。 我相信您附近的商店很快就會(huì)堆滿所有俗氣的圣誕節(jié)商品。

But before you groan too much, there are a number of other options; these include: cli, phing, tap, and NyanCat.

但是,在您吟過多之前,還有許多其他選擇。 其中包括: cli,phing,tap和NyanCat 。

With that done, let’s set code coverage to export to HTML format. Create a new directory structure, /coverage/html, off the root directory of your project.

完成后,讓我們?cè)O(shè)置代碼覆蓋率以導(dǎo)出為HTML格式。 在項(xiàng)目的根目錄下創(chuàng)建一個(gè)新的目錄結(jié)構(gòu)/coverage/html 。

Then, copy vendor/atoum/resources/configurations/runner/coverage.php.dist to the root directory of your project as coverage.php. In there, we need to update the configuration entries, listed in the code snippet below:

然后,將vendor/atoum/resources/configurations/runner/coverage.php.dist復(fù)制到項(xiàng)目的根目錄,作為coverage.php 。 在這里,我們需要更新配置條目,列在下面的代碼片段中:

<?phpuse \mageekguy\atoum\report\fields\runner\coverage;$coverageHtmlField = new coverage\html('Your project name','/path/to/destination/directory' ); $coverageHtmlField->setRootUrl('http://url/of/web/site'); $coverageTreemapField = new coverage\treemap('Your project name','/path/to/destination/directory' );$coverageTreemapField->setTreemapUrl('http://url/of/treemap')->setHtmlReportBaseUrl($coverageHtmlField->getRootUrl());$script->addDefaultReport()->addField($coverageHtmlField)->addField($coverageTreemapField);

After that, we’re ready to start writing tests.

之后,我們準(zhǔn)備開始編寫測試。

我們的第一個(gè)測試 (Our First Test)

In my application’s source code, I have an entity called Journal, which stores a daily food intake entry which I write to track what I’ve eaten, and how I’ve reacted to it. In it, I have the following function:

在應(yīng)用程序的源代碼中,我有一個(gè)名為Journal的實(shí)體,該實(shí)體存儲(chǔ)我每天編寫的食物攝入條目,以跟蹤我吃了什么以及對(duì)它的React。 在其中,我具有以下功能:

public function populate(array $data) {if (empty($data)) {throw new HydrationException();}$this->id = $data['id'];$this->userId = $data['userId'];$this->entry = $data['entry'];$this->created = $data['created'];$this->updated = $data['updated']; }

You can see that it takes one parameter, $data, which is an array. If $data is empty, then it throws a HydrationException, which is a custom exception.

您可以看到它帶有一個(gè)參數(shù)$data ,它是一個(gè)數(shù)組。 如果$data為空,則拋出HydrationException ,這是一個(gè)自定義異常。

If $data’s not empty, then it populates each of the five private member variables using the key of the same name from $data. Nothing too special, yet enough to test with.

如果$data不為空,則使用$data中具有相同名稱的鍵填充五個(gè)私有成員變量中的每一個(gè)。 沒什么特別的,但足以進(jìn)行測試。

To create the first test, by default, we don’t follow the same approach which you might find in many other projects such as Zend Framework, Symfony, or Laravel.

默認(rèn)情況下,要?jiǎng)?chuàng)建第一個(gè)測試,我們不遵循您在許多其他項(xiàng)目(例如Zend Framework,Symfony或Laravel)中可能發(fā)現(xiàn)的相同方法。

Out of the box, atoum suggests a different style – creating a directory called tests/units, relative to the classes which you are going to test.

開箱即用,atoum提出了另一種風(fēng)格–創(chuàng)建一個(gè)名為tests/units的目錄,相對(duì)于要測試的類。

So, in the case of my Journal entity, which is located at src/App/Entity, I’d then create a new test class, also named Journal, in tests/units/Entity/Journal.php.

因此,對(duì)于我的Journal實(shí)體(位于src/App/Entity ,我將在tests/units/Entity/Journal.php創(chuàng)建一個(gè)新的測試類,也稱為Journal 。

Then, I’d give it a PSR-4 type namespace, namespace App\Entity\tests\units;. After that, I only need to use the atoum namespace and I can begin writing tests.

然后,給它一個(gè)PSR-4類型的名稱空間, namespace App\Entity\tests\units; 。 之后,我只需要使用atoum命名空間,就可以開始編寫測試了。

<?phpnamespace App\Entity\tests\units;use App\Entity\Exception\HydrationException; use App\Entity\Journal as Entity; use Faker\Factory; use Faker\Generator; use atoum;

I’ll start off by including all of the namespaces and classes which the test will need. After that, as with PHPUnit, I’ve created the first test, following the standard naming style of prefixing a test with test, which identifies it as a test.

首先,我將包括測試所需的所有名稱空間和類。 之后,與PHPUnit一樣,我按照將測試加前綴test的標(biāo)準(zhǔn)命名樣式創(chuàng)建了第一個(gè)測試,該test其標(biāo)識(shí)為測試。

Then, using the excellent Faker library, I’ve created an array of test data to populate the entity with. With that ready, we next step through the test.

然后,使用出色的Faker庫 ,我創(chuàng)建了一組測試數(shù)據(jù)來填充實(shí)體。 準(zhǔn)備就緒后,我們下一步將通過測試。

class Journal extends atoum {public function testHydration(){$faker = Factory::create();$data = ['id' => $faker->randomDigitNotNull,'userId' => $faker->randomDigitNotNull,'entry' => $faker->text,'created' => $faker->dateTime,'updated' => $faker->dateTime,];$this->given($entity = new Entity())->when($entity->populate($data))->then->integer($entity->getId())->isEqualTo($data['id']);} }

Atoum has a number of styles for writing tests. You can use the BDD/Gherkin-style of given -> when -> then, as I have above. Or, you can take a more direct approach, which you can see in the example below.

Atoum有多種編寫測試的樣式。 您可以使用given -> when -> then 的BDD / Gherkin樣式 ,如我上面所述。 或者,您可以采用更直接的方法,在下面的示例中可以看到。

faker = Factory::create(); $entity = new Entity(); $data = ['id' => faker->randomDigitNotNull,'userId' => faker->randomDigitNotNull,'entry' => faker->text,'created' => faker->dateTime,'updated' => faker->dateTime, ]; $entity->populate($data); $this->object($entity)->integer($entity->getId())->isEqualTo($this->data['id']);

Personally, whilst a bit more verbose, I prefer the former style to the latter. I find it more readable and intuitive. The choice is yours.

就個(gè)人而言,雖然有點(diǎn)冗長,但我更喜歡前一種風(fēng)格。 我發(fā)現(xiàn)它更具可讀性和直觀性。 這是你的選擇。

Now let’s look a bit deeper at the tests. We used the given method to establish the preamble, or set up the system under test (SUT), that being a Journal entity object. We then used the when method to run the SUT and get a result, specifically by calling its populate() method to hydrate it with test data.

現(xiàn)在讓我們對(duì)測試進(jìn)行更深入的了解。 我們使用given方法來建立作為Journal實(shí)體對(duì)象的前導(dǎo)或建立被測系統(tǒng)(SUT)。 然后,我們使用when方法來運(yùn)行SUT并獲得結(jié)果,特別是通過調(diào)用其populate()方法將其與測試數(shù)據(jù)進(jìn)行水合。

With the object hydrated, we then computed the test verdict using then() along with two assertions, comparing the post-state of the object, with the data used to hydrate it. In this assessment of the SUT’s post-state, we’re only checking if the Journal’s id member variable, returned by calling $entity->getId(), is both an integer, and is equal to the value in $data’s id element.

在水化對(duì)象后,我們使用then()以及兩個(gè)斷言來計(jì)算測試結(jié)論,將對(duì)象的后狀態(tài)與用于水化的數(shù)據(jù)進(jìn)行比較。 在對(duì)SUT后狀態(tài)的評(píng)估中,我們僅檢查通過調(diào)用$entity->getId()返回的Journal的id成員變量是否既是整數(shù),又等于$data的值, s id元素。

As there are a number of properties which can be set on the object, we could have continued to use the fluent interface to test them all, such as in the example below:

由于可以在對(duì)象上設(shè)置許多屬性,因此我們可以繼續(xù)使用fluent接口來測試它們,例如下面的示例:

$this->object($entity)->integer($entity->getId())->isEqualTo(data['id'])->integer($entity->getUserId())->isEqualTo(data['userId'])->string($entity->getEntry())->isEqualTo(data['entry'])->dateTime($entity->getCreated())->isEqualTo(data['created'])->dateTime($entity->getUpdated())->isEqualTo(data['updated']);

Here we’ve used another assertion method, string(), on the values which return a string, and dateTime() for the dateTime values, instead of integer(). However, as you might expect from a comprehensive testing library, there are a range of other assertion options.

在這里,我們?cè)诜祷刈址闹瞪鲜褂昧肆硪粋€(gè)斷言方法string() ,對(duì)于dateTime值使用了dateTime dateTime() ,而不是integer() 。 但是,正如您可能希望從全面的測試庫中獲得的那樣,還有許多其他斷言選項(xiàng)。

運(yùn)行測試 (Running the Tests)

With the test ready, it’s time to run it. From the command line, in the root directory of your project, run the following command:

準(zhǔn)備好測試之后,就該運(yùn)行它了。 在命令行中,在項(xiàng)目的根目錄中,運(yùn)行以下命令:

./vendor/bin/atoum -c coverage.php -f src/App/Entity/tests/units/Journal.php

This will run just the test that we’ve created, using the -f switch, and enable code coverage using the -c switch. When you run it, you should see output similar to that in the image below – ho ho ho!

這將使用-f開關(guān)僅運(yùn)行我們創(chuàng)建的測試,并使用-c開關(guān)啟用代碼覆蓋率。 運(yùn)行它時(shí),您應(yīng)該看到與下圖類似的輸出– ho ho ho

If you want though, feel free to change the output style to anything else; my personal favorite is NyanCat. Who wouldn’t love crazy output like this:

如果需要,請(qǐng)隨時(shí)將輸出樣式更改為其他樣式。 我個(gè)人最喜歡的是NyanCat 。 誰不會(huì)喜歡這樣的瘋狂輸出:

代碼覆蓋率 (Code Coverage)

Now that we have a working test, let’s take a closer look at the code coverage reporting, starting with the command-line output. We can see, in the abridged sample output below, that for the class tested, App\Entity\Journal, it has total code coverage of 93.33%. This is because its __toString method has no coverage at all.

既然我們已經(jīng)有了一個(gè)有效的測試,讓我們從命令行輸出開始,仔細(xì)看一下代碼覆蓋率報(bào)告。 我們可以在下面的簡短示例輸出中看到,對(duì)于測試的類App\Entity\Journal ,其總代碼覆蓋率為93.33%。 這是因?yàn)槠鋉_toString方法完全沒有覆蓋范圍。

> Code coverage value: 93.33% => Class App\Entity\Journal: 93.33% ==> App\Entity\Journal::__toString(): 0.00% Success (1 test, 2/2 methods, 0 void method, 0 skipped method, 13 assertions)! > Code coverage value: 93.33% => Class App\Entity\Journal: 93.33% ==> App\Entity\Journal::__toString(): 0.00% > Running duration: 0.15 second.

Now let’s look at the HTML coverage output. Firing up PHP’s web server using the command php -S 127.0.0.1:8080 -t ./coverage/html, we can see the HTML report at http://localhost:8080:

現(xiàn)在讓我們看一下HTML coverage輸出。 使用命令php -S 127.0.0.1:8080 -t ./coverage/html啟動(dòng)PHP的Web服務(wù)器,我們可以在http://localhost:8080看到HTML報(bào)告:

There, we see a list of all classes covered in the coverage report and their level of code coverage. By clicking on it, we then see how the code coverage level is assessed.

在這里,我們可以看到覆蓋率報(bào)告中涵蓋的所有類的列表及其代碼覆蓋率級(jí)別。 通過單擊它,我們?nèi)缓罂吹饺绾卧u(píng)估代碼覆蓋率級(jí)別。

Each of the methods, bar __toString(), has 100% code coverage. Looking through the source, we see, highlighted in green, what has been covered, and in orange what is yet to be.

每個(gè)方法bar __toString()都有100%的代碼覆蓋率。 從源頭上看,我們看到綠色覆蓋了已覆蓋的內(nèi)容,橙色尚未顯示。

This is why using a generated report is much more effective, because it provides a better understanding than the command line output.

這就是為什么使用生成的報(bào)告要有效得多的原因,因?yàn)榕c命令行輸出相比,它提供了更好的理解。

調(diào)試測試 (Debugging Tests)

But, what if something goes wrong? Say for example, I hadn’t set a value for id in the $data array, or I expected it to match a different value. Given that, when I run it I’d expect to see an error like this one:

但是,如果出現(xiàn)問題怎么辦? 例如,我沒有在$data數(shù)組中為id設(shè)置值,或者我希望它與其他值匹配。 鑒于此,當(dāng)我運(yùn)行它時(shí),我希望看到這樣的錯(cuò)誤:

In this situation, it’s not always easy to see what’s going wrong. However, atoum offers a couple of options in this case. These come in the form of the dump(), stop(), and executeOnFailure() methods.

在這種情況下,很難總是看到問題所在。 但是,在這種情況下,atoum提供了兩種選擇。 這些以dump() , stop()和executeOnFailure()方法的形式出現(xiàn)。

I’ll focus on dump(), but feel free to have a look at the other two on your own. Dump, similar to PHP’s var_dump(), will dump out the contents of a variable to stdout, as part of running the test. To use it, I just have to add a call to it in the test, like so:

我將重點(diǎn)介紹dump() ,但您可以自己看看其他兩個(gè)。 類似于PHP的var_dump() ,轉(zhuǎn)儲(chǔ)會(huì)將變量的內(nèi)容轉(zhuǎn)儲(chǔ)到stdout ,作為運(yùn)行測試的一部分。 要使用它,我只需要在測試中向它添加一個(gè)調(diào)用,如下所示:

$this->dump($entity)->object($entity)->integer($entity->getId())->isEqualTo(100000000000)->integer($entity->getUserId())->isEqualTo($data['userId'])->string($entity->getEntry())->isEqualTo($data['entry'])->dateTime($entity->getCreated())->isEqualTo($data['created'])->dateTime($entity->getUpdated())->isEqualTo($data['updated'])->dump($entity)

There is one thing to remember, though: the debug methods will only work if the tests are run in debug mode. To do that, we have to use either the -d or --debug switches when calling atoum. When you do so, you’ll see output similar to the following.

但是,要記住一件事:僅當(dāng)測試以調(diào)試模式運(yùn)行時(shí),調(diào)試方法才有效。 為此,我們必須在調(diào)用atoum時(shí)使用-d或--debug開關(guān)。 這樣做時(shí),您將看到類似于以下的輸出。

=> In App\Entity\tests\units\Journal::testSimpleHydration(): /Users/settermjd/Workspace/settermjd/health-monitor/vendor/atoum/atoum/classes/test.php:429: class App\Entity\Journal#195 (5) {private $id =>int(1)private $userId =>int(3)private $entry =>string(165) "Reiciendis quod at voluptatem cupiditate error exercitationem at.Deserunt quos vero omnis est aliquam qui. Esse at quo dolorum fugit.Qui voluptas omnis amet nihil."private $created =>class DateTime#194 (3) {public $date =>string(26) "1992-06-18 01:55:51.000000"public $timezone_type =>int(3)public $timezone =>string(3) "UTC"}private $updated =>class DateTime#193 (3) {public $date =>string(26) "2016-09-28 08:27:18.000000"public $timezone_type =>int(3)public $timezone =>string(3) "UTC"} }

You can see here the structure of the Journal entity and compare what you expected to receive to what was received.

您可以在此處查看Journal實(shí)體的結(jié)構(gòu),并將您希望收到的內(nèi)容與收到的內(nèi)容進(jìn)行比較。

利用注釋 (Making Use of Annotations)

針對(duì)不同版本PHP進(jìn)行測試 (Testing against Different Versions of PHP)

What we’ve seen so far, could be argued, is fairly standard for a unit testing library. Now let’s finish up our initial tour of atoum, by looking at how it starts to come in to its own.

到目前為止,我們可以看到,對(duì)于單元測試庫來說,這是相當(dāng)標(biāo)準(zhǔn)的。 現(xiàn)在,讓我們看一下它是如何開始成為自己的原子的,從而結(jié)束我們的初始原子之旅。

Say that you’re starting to refactor some of the existing libraries of your application to work with PHP 7, but they’re not ready for introduction in to the main branch, as that’s still highly dependent on PHP 5.6. Atoum can filter out tests for those aspects of code that can’t be tested against a given PHP runtime by using the @php annotation.

假設(shè)您已經(jīng)開始重構(gòu)應(yīng)用程序中的某些現(xiàn)有庫以與PHP 7一起使用,但是它們尚未準(zhǔn)備好引入主分支,因?yàn)樗匀桓叨纫蕾嘝HP 5.6。 通過使用@php批注,Atoum可以篩選出無法針對(duì)給定PHP運(yùn)行時(shí)進(jìn)行測試的那些代碼方面的測試。

Atoum, via the use of annotations, lets you specify the version of PHP in effect for a test to run. It does this via the @php annotation. For the methods that require PHP 7, we specify @php 7.0 in the PhpDoc block header. For all the other methods, we can leave them as is. If an entire test class required PHP 7, we could use this in the class’ annotation instead of using it for every test contained in the class.

通過使用批注,Atoum允許您指定有效PHP版本以運(yùn)行測試。 它通過@php注釋來實(shí)現(xiàn)。 對(duì)于需要PHP 7的方法,我們?cè)赑hpDoc塊標(biāo)題中指定@php 7.0 。 對(duì)于所有其他方法,我們可以保持原樣。 如果整個(gè)測試類都需要PHP 7,則可以在類的批注中使用它,而不是對(duì)類中包含的每個(gè)測試都使用它。

Now this might seem like a strange thing to want to do. Naturally, if you have a class which makes more than a passing use of PHP 7, such as scalar type hints, if you run the script (or application) with anything other than PHP 7, then it will result in fatal errors. So I would suggest that this feature would be used when you are working through a porting process.

現(xiàn)在,這似乎是一件很奇怪的事情。 自然地,如果您擁有一個(gè)類,而不是僅僅使用了PHP 7,例如標(biāo)量類型提示,那么,如果您使用PHP 7以外的任何內(nèi)容運(yùn)行腳本(或應(yīng)用程序),則將導(dǎo)致致命錯(cuò)誤。 因此,我建議您在移植過程中使用此功能。

擴(kuò)展名 (?Extensions)

Now what if you need specific extensions for a test method to work? As with @php, we can specify @extensions. Say we need one, or a combination of the mbstring, PDO, and Xdebug extensions. We could use the @extensions annotation, as below, to require these extensions.

現(xiàn)在,如果您需要特定的擴(kuò)展名才能使測試方法起作用,該怎么辦? 與@php ,我們可以指定@extensions 。 假設(shè)我們需要mbstring,PDO和Xdebug擴(kuò)展之一,或?qū)⑵浣M合。 我們可以使用@extensions批注(如下所示)來要求這些擴(kuò)展名。

/*** @extensions mbstring* Or* @extensions pdo xdebug*/

Through making astute use of these annotations, we can start to create more sophisticated tests, as well as a more sophisticated test automation infrastructure; one which allows us to seamlessly migrate our application from PHP 5.6, through to 7.0 (or 7.1).

通過巧妙地使用這些注釋,我們可以開始創(chuàng)建更復(fù)雜的測試以及更復(fù)雜的測試自動(dòng)化基礎(chǔ)結(jié)構(gòu)。 一種允許我們將應(yīng)用程序從PHP 5.6無縫移植到7.0(或7.1)的方法。

結(jié)論 (In Conclusion)

This has been a high-level overview of atoum, with some deep diving into a number of areas. There is far more on offer which I’ve not been able to cover, such as the extensions.

這是對(duì)原子層的高層次概述,并深入了許多領(lǐng)域。 還有很多我無法涵蓋的內(nèi)容,例如擴(kuò)展名。

There’s [the visibility extension])(https://github.com/atoum/visibility-extension) which allows for testing protected and private methods. There’s the ruler extension which allows for filtering the tests which are run. Then there’s the Blackfire extension, which allows you to write Blackfire test suites using atoum.

[可見性擴(kuò)展])(https://github.com/atoum/visibility-extension)允許測試受保護(hù)的方法和私有方法。 標(biāo)尺擴(kuò)展允許過濾運(yùn)行的測試。 然后是Blackfire擴(kuò)展程序 ,該擴(kuò)展程序允許您使用atoum編寫B(tài)lackfire測試套件。

On top of that, there’s CI server integration, as atoum can be integrated with Hudson, Jenkins, and TravisCI, amongst others.

最重要的是,還有CI服務(wù)器集成 ,因?yàn)閍toum可以與Hudson,Jenkins和TravisCI等集成。

But don’t let this introduction be all that you discover. Whilst atoum is newer than PHP’s de facto testing library, PHPUnit, I think it’s a library well worth considering — especially if the way PHPUnit works isn’t quite how you like it.

但是,不要讓此介紹成為您發(fā)現(xiàn)的全部。 盡管atoum比PHP的事實(shí)上的測試庫PHPUnit更新,但我認(rèn)為這是一個(gè)值得考慮的庫-特別是如果PHPUnit的工作方式并不完全符合您的喜好。

Keep in mind that this project has a strong community, several release managers, and a core development team. There’s a lot to it, and getting started is not as intuitive as I’d have liked. However — once you’re up and running, it can really grow on you!

請(qǐng)記住,這個(gè)項(xiàng)目有一個(gè)強(qiáng)大的社區(qū),幾個(gè)發(fā)布經(jīng)理和一個(gè)核心開發(fā)團(tuán)隊(duì)。 有很多東西,而且入門并不像我想要的那樣直觀。 但是,一旦您啟動(dòng)并運(yùn)行,它便可以真正在您身上成長!

If this tutorial’s whet your appetite, be sure to check out the online documentation, where you can find much more information about all aspects of the library.

如果您對(duì)本教程感興趣,請(qǐng)務(wù)必查看在線文檔 ,在該文檔中您可以找到有關(guān)庫的所有方面的更多信息。

What do you think about atoum? Did you give it a go? Will you?

您如何看待原子核? 你有去嗎? 你會(huì)?

  • https://github.com/atoum/bdd-extension

    https://github.com/atoum/bdd-extension
  • https://github.com/atoum/visibility-extension

    https://github.com/atoum/visibility-extension
  • https://github.com/atoum/json-schema-extension

    https://github.com/atoum/json-schema-extension
  • https://github.com/atoum/ruler-extension

    https://github.com/atoum/ruler-extension
  • https://github.com/atoum/atoum

    https://github.com/atoum/atoum

翻譯自: https://www.sitepoint.com/testing-php-code-with-atoum-an-alternative-to-phpunit/

總結(jié)

以上是生活随笔為你收集整理的使用Atoum测试PHP代码-PHPUnit的替代方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。