一个简单的php测试系统,Pest 一个令人愉快的PHP测试框架,专注于简单性

banner.png

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6e756e6f6d616475726f2f706573742f6d61737465722e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f706573742f642f746f74616c2e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f706573742f762f737461626c652e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f706573742f6c6963656e73652e737667

Pest was created by, and is maintained by PHP testing solution. Works out of the box for any PHPUnit project.

🚀 Installation & Usage

Requires PHP 7.2+ and phpunit 8.1+

First, Install Pest using Composer:

composer require nunomaduro/pest --dev

Then, create a file named tests/sum.php. This will contain our actual test:

test('adds 1 + 2 to equal 3', function () {

assertEquals(3, Math::sum(1,2));

});

Then, as usual, if you don't have it yet, create your phpunit.xml.dist.

Finally, run vendor/bin/pest and pest will print this message:

PASS ./sum.php

✓ adds 1 + 2 to equal 3 (5ms)

📚 Documentation

Pest aims to work out of the box, config free, on most PHP/PHPUnit projects.

Our goal is create a delightful PHP Testing Framework with a focus on simplicity - with ideas coming from a line between

Writing tests

All you need in a test file is the test or it method which runs a test. For example, let's say there's a function inchesOfRain() that should be 0. Your whole test could be:

test('did not rain', function () {

assertEquals(0, Weather::inchesOfRain());

});

# Or, also under the alias `it`

it('did not rain', function () {

assertEquals(0, Weather::inchesOfRain());

});

Using Assertions

Pest uses "assertions" to let you test values in different ways.

it('has something', (function () {

assertTrue(true);

assertFalse(false);

assertCount(1, ['foo']);

assertEmpty([]);

assertEquals('bar', 'bar');

assertStringContainsString('bar', 'foobarbaz');

// ...

});

Setup and Teardown

Often while writing tests you have some setup work that needs to happen before tests run, and you have some finishing work that needs to happen after tests run. Pest provides helper functions to handle this.

# Runs before each test on this file

beforeEach(function () {

Database::migrate();

});

# Runs after each test on this file

afterEach(function () {

Database::delete();

});

test('city database has Vienna', function () {

assertTrue(City::exists('Vienna'));

});

test('city database has San Juan', function () {

assertTrue(City::exists('San Juan'));

});

One-Time Setup

In some cases, you only need to do setup once, at the beginning of a file. This can be especially bothersome when the setup is asynchronous, so you can't just do it inline. Pest provides beforeAll and afterAll to handle this situation.

# Runs before the first test of the file

beforeAll(function () {

Database::migrate();

});

# Runs after the last test of the file

afterAll(function () {

Database::delete();

});

test('city database has Vienna', function () {

assertTrue(City::exists('Vienna'));

});

test('city database has San Juan', function () {

assertTrue(City::exists('San Juan'));

});

This may help to illustrate the order of execution:

beforeAll(function () { echo 'beforeAll'); };

afterAll(function () { echo 'afterAll'); };

beforeEach(function () { echo 'beforeEach'); };

afterEach(function () { echo 'afterEach'); };

test('', function () { echo 'test 1'); };

test('', function () { echo 'test 2'); };

// beforeAll

// beforeEach

// test 1

// afterEach

// beforeEach

// test 2

// afterEach

// afterAll

Mocks

The given closure to the test|it method is bound to a typical PHPUnit\Framework\TestCase. For mocks, you can optionally create a mock using the $this->createMock method.

interface Foo

{

public function bar(): int;

}

it('works fine with mocks', function () {

$mock = $this->createMock(Foo::class);

$mock->expects($this->once())->method('bar')->willReturn(2);

assertEquals(2, $mock->bar());

});

Migrating to Pest from PHPUnit

No migration need. It just works.

Configuration

Pest uses your base phpunit.xml configuration file.

💖 Support the development

Do you like this project? Support it by donating

PayPal: Donate

Patreon: Donate

Pest is open-sourced software licensed under the MIT license.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值