如何在 PHPUnit 中以编程方式对测试函数进行排序?

发布于 2024-10-29 02:22:33 字数 392 浏览 3 评论 0原文

我正在使用 PHPUnit 来测试一个具有许多函数的类。 PHPUnit框架从上到下运行测试函数。

问题是:如何按指定的顺序运行测试函数,而不需要在源代码中重新排序。

为了澄清这个问题,假设我们有 5 个测试函数;

  • testFunc1
  • testFunc2
  • testFunc3
  • testFunc4
  • testFunc5

框架将运行 testFunc1,然后运行 ​​testFunc2,直到到达 testFunc5。

但是,我想运行 testFunc3 然后 testFunc1 然后 testFunc5 然后 testFunc2 然后 testFunc4 而不在源文件中重新排序它们。

I'm using PHPUnit to test a class that has many functions.
The PHPUnit framework runs the test functions from the top to the bottom.

The question is: How can I run the test functions in a specified order without reorder then in the source code.

To clarify the issue, imagine we have 5 test functions;

  • testFunc1
  • testFunc2
  • testFunc3
  • testFunc4
  • testFunc5

The framework will run testFunc1 then testFunc2 until it reaches testFunc5.

However, I want to run testFunc3 then testFunc1 then testFunc5 then testFunc2 then testFunc4 without reordering them in the source file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤城病女 2024-11-05 02:22:33

PHPUnit 将按照在 *_TestCase 类中编写的确切顺序执行测试。

这些测试中的每一项都应该能够独立运行,而不依赖于在它之前执行的其他测试。

如果您在针对数据库进行测试时遇到问题,我建议您使用如下所示的方法:

class MyTest extends PHPUnit_Framework_TestCase {

    public function setUp() {
        // REPLACE INTO testDb (ID, NAME, VALUE) VALUES (1001000, 'testing', 'value')
        $this->db = $db_connection;
    }

    public function tearDown() {
        // DELETE FROM testDb WHERE ID > 10010000 // or something like this
    }

    public function testSelect() {
        $this->assertSame("value", $this->db->getId(100100));
    }

    /**
     * @depends testSelect
     */
    public function testInsert() {
        $this->db->insertById(1001111, "mytest", "myvalue");
        $this->db->getId(1001111);
    }

    /**
     * @depends testSelect
     */
    public function testDelete() {
        $this->db->deleteById(1001000);
        $this->assertNull($this->db->getId(10010000);
    }

    // and so on
}

setUp() 方法将在每个测试用例之前运行,并确保大多数测试用例需要的所有值都在那里,>tearDown() 将在测试套件之后进行清理。

@depends 注释将确保当选择测试失败时不会运行插入测试。 (如果您无法加载值,则插入新值并获取这些值将无法工作,无需尝试)。

对于这还检查了测试依赖项的手册

PHPUnit will execute the tests in the exact order they are written in your *_TestCase class.

Every one of those tests should be able to run in isolation and not depend on some other test beeing executed before it.

If you have issues testing against a Database I'd suggest using somethig like this:

class MyTest extends PHPUnit_Framework_TestCase {

    public function setUp() {
        // REPLACE INTO testDb (ID, NAME, VALUE) VALUES (1001000, 'testing', 'value')
        $this->db = $db_connection;
    }

    public function tearDown() {
        // DELETE FROM testDb WHERE ID > 10010000 // or something like this
    }

    public function testSelect() {
        $this->assertSame("value", $this->db->getId(100100));
    }

    /**
     * @depends testSelect
     */
    public function testInsert() {
        $this->db->insertById(1001111, "mytest", "myvalue");
        $this->db->getId(1001111);
    }

    /**
     * @depends testSelect
     */
    public function testDelete() {
        $this->db->deleteById(1001000);
        $this->assertNull($this->db->getId(10010000);
    }

    // and so on
}

The setUp() method will be run before every testcase and make sure all the values most testcases need are there, the tearDown() will clean up after the testsuite.

The @depends annotation will make sure that the insert test isn't run when the select test fails. (If you can't load values then inserting new ones and getting those can't work ether, no need to try it).

For that also check the manual on test dependencies

何必那么矫情 2024-11-05 02:22:33

单元测试的全部要点实际上就在名称本身中:单元测试。它们独立运行,彼此之间没有任何依赖关系。如果你的测试代码正确,执行顺序应该不重要。

如果是数据库问题,请确保在每次测试之前都有一个干净的数据库。

The whole point of unit tests are actually in the name itself, Unit Testing. They function on their own and have no dependencies whatsoever on each other. If you code your tests right, order of execution should not matter.

If it is a matter of a database issue, make sure you have a clean database before every test.

月亮坠入山谷 2024-11-05 02:22:33

现在除了对文件中的函数重新排序之外没有其他办法。有一个功能请求使用 @depends 注释来重新排序测试,并且 PHPUnit 作者已经表达了这样做的愿望。您可以在对请求发表评论 /sebastianbergmann/phpunit" rel="nofollow">PHPUnit 的 github 跟踪器。

Right now there's no way to do it short of reordering the functions in the file. There's a feature request to use the @depends annotations to reorder the tests, and the PHPUnit author has expressed a desire to do it. You can comment on the request at PHPUnit's github tracker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文