PHPackages                             fastknife/think-testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. fastknife/think-testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

fastknife/think-testing
=======================

单元测试

v1.0.0(1y ago)018Apache-2.0PHPPHP &gt;=7.1.0

Since Jan 21Pushed 1y agoCompare

[ Source](https://github.com/fastknifes/think-testing)[ Packagist](https://packagist.org/packages/fastknife/think-testing)[ RSS](/packages/fastknife-think-testing/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

### 介绍

[](#介绍)

单元测试

使用的时候直接在项目目录下执行 `php think unit`

> 该项目改自 shuipf/think-testing

### 相关手册

[](#相关手册)

`配置实例：https://github.com/top-think/think`

`框架核心库：https://github.com/top-think/framework`

### 用例

[](#用例)

#### 配置

[](#配置)

请在项目根目录下创建 tests目录，并创建phpunit.xml,内容如下：

```

            ./tests/

            app/

```

#### 基本格式

[](#基本格式)

- 测试类命名： 类名 + Test ， eg FooClassTest
- 测试方法命名： test + 方法名 , eg testFoo

> 也可以使用注释 @test 来标注需要测试的方法

```
class SampleTest extends TestCase
{
    public function testSomething()
    {
        $this->assertTrue(true, 'This should already work.');
    }

    /**
     * @test
     */
    public function something()
    {
        $this->assertTrue(true, 'This should already work.');
    }
}
```

#### 常用命令

[](#常用命令)

- 过滤文件 php think unit --filter \*\*\*

```
--filter 'TestNamespace\\TestCaseClass::testMethod'

--filter 'TestNamespace\\TestCaseClass'

--filter TestNamespace

--filter TestCaseClass

--filter testMethod
```

- 分组 php think unit --group \*\*\* 可以用 @group 标注来标记某个case属于一个或多个组，就像这样：

```
class MyTest extends TestCase{
    /**
     * @group specification
     */
    public function testSomething(){
    }

    /**
     * @group regresssion
     * @group bug2204
     */
    public function testSomethingElse(){
    }
}
```

#### 测试依赖 (@depends)

[](#测试依赖-depends)

有一些测试方法需要依赖于另一个测试方法的返回值，此时需要使用测试依赖。测试依赖 通过注释 @depends 来标记。 下列中， depends 方法的 return 值作为 testConsumer 的参数传入

```
class MultipleDependenciesTest extends TestCase
{
    public function testProducerFirst()
    {
        $this->assertTrue(true);
        return 'first';
    }

    public function testProducerSecond()
    {
        $this->assertTrue(true);
        return 'second';
    }

    /**
     * @depends testProducerFirst
     * @depends testProducerSecond
     */
    public function testConsumer($a, $b)
    {
        $this->assertSame('first', $a);
        $this->assertSame('second', $b);
    }
}
```

#### 数据提供器 (@dataProvider)

[](#数据提供器-dataprovider)

在依赖中，所依赖函数的返回值作为参数传入测试函数。除此之外，我们也可以用数据提供器来定义传入的数据。

```
class DataTest extends TestCase
{
    /**
     * @dataProvider additionProvider
     */
    public function testAdd($a, $b, $expected)
    {
        $this->assertSame($expected, $a + $b);
    }

    public function additionProvider()
    {
        return [
            'adding zeros' => [0, 0, 0], // 0 + 0 = 0 pass
            'zero plus one' => [0, 1, 1], // 0 + 1 = 1 pass
            'one plus zero' => [1, 0, 1], // 1 + 0 = 1 pass
            'one plus one' => [1, 1, 2], // 1 + 1 = 2 pass
        ];
    }
}
```

#### 测试异常 (expectException)

[](#测试异常-expectexception)

需要在测试方法的开始处声明断言，然后执行语句。而不是调用后再声明 也可以通过注释来声明 @expectedException, @expectedExceptionCode, @expectedExceptionMessage, @expectedExceptionMessageRegExp

```
class ExceptionTest extends TestCase
{
    public function testException()
    {
        $this->expectException(\Exception::class);

        throw new \Exception('test');
    }

    /**
     * @throws \Exception
     * @test
     * @expectedException \Exception
     */
    public function exceptionExpect()
    {
        throw new \Exception('test');
    }
}
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance42

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

483d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8088854a50a2af2707e89edeaf062e9f09054e98db166610294a989c304e7626?d=identicon)[miwei230](/maintainers/miwei230)

---

Top Contributors

[![shuipf](https://avatars.githubusercontent.com/u/2285144?v=4)](https://github.com/shuipf "shuipf (6 commits)")[![fastknifes](https://avatars.githubusercontent.com/u/20872930?v=4)](https://github.com/fastknifes "fastknifes (2 commits)")

### Embed Badge

![Health badge](/badges/fastknife-think-testing/health.svg)

```
[![Health](https://phpackages.com/badges/fastknife-think-testing/health.svg)](https://phpackages.com/packages/fastknife-think-testing)
```

###  Alternatives

[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2021.0M277](/packages/drupal-core-dev)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[juampi92/test-seo

Easy way to test your SEO

26341.0k](/packages/juampi92-test-seo)[topthink/think-testing

4392.2k8](/packages/topthink-think-testing)[webmozarts/strict-phpunit

Enables type-safe comparisons of objects in PHPUnit

31252.7k5](/packages/webmozarts-strict-phpunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
