简体   繁体   English

有没有办法在 Codeception 中控制测试顺序?

[英]Is there a way to control tests order in Codeception?

I just started using Codeception after years of writing unit tests in plain PHPUnit .在用PHPUnit编写单元测试多年后,我才开始使用Codeception One thing that is bugging me, that I can't find a way to control the order in which the tests are invoked.一件事困扰着我,我找不到一种方法来控制调用测试的顺序。

In pure old PHPUnit I was building the test structure manually like this:在纯旧的PHPUnit我像这样手动构建测试结构:

$suite = new PHPUnit_Framework_TestSuite();
$suite->addTest('MyFirstTest');
$suite->addTest('MySecondTest');

and the test would be invoked in the order which they were added to the suite.并且测试将按照它们被添加到套件的顺序被调用。 Codeception on the other hand seems to be iterating through directories and running every test it can find.另一方面, Codeception似乎在遍历目录并运行它可以找到的每个测试。

I would like to be able to control the order of the tests on two levels:我希望能够在两个级别上控制测试的顺序:

  1. The order in which different kind of tests are invoked (ie I would like to run unit tests before acceptance tests )调用不同类型测试的顺序(即我想在acceptance tests之前运行unit tests acceptance tests
  2. I would like to control the order of tests invoked in specific test type (in similar manner the PHPUnit builds suites)我想控制在特定测试类型中调用的测试顺序(以类似的方式PHPUnit构建套件)

Ad.广告。 2: Let's say I have two tests in acceptance directory: 2:假设我在acceptance目录中有两个测试:

AbcCept.php
WebGuy.php
XyzCept.php

I want to be able to run the XyzCept.php before AbcCept.php .我希望能够在XyzCept.php之前运行AbcCept.php Is this even possible?这甚至可能吗?

And to anticipate picky comments: yes, I know that tests should be able to run in any order, and not depend on each other, but that's not what I'm asking.并期待挑剔的评论:是的,我知道测试应该能够以任何顺序运行,而不是相互依赖,但这不是我要问的。

Files get sorted by name (I assume we are talking about files from the same directory).文件按名称排序(我假设我们正在谈论来自同一目录的文件)。 In other words if you need to run the test XyzCept.php before AbcCept.php you re-name the XyzCept.php to, let's say, AazCept.php .换句话说,如果你需要运行测试XyzCept.php之前AbcCept.php您重新命名XyzCept.php到,比方说, AazCept.php

You can create a batch script that runs the tests in any order you like, eg,您可以创建一个批处理脚本,以您喜欢的任何顺序运行测试,例如,

cd \path\to\test\dir
codecept run unit
codecept run acceptance

or

cd \path\to\test\dir
codecept run unit
codecept run acceptance XycCept.php
codecept run acceptance AbcCept.php

See the "Run" command on this page .请参阅此页面上的“运行”命令。

For the ones who are searching for a proper solution and bump into this issue.对于那些正在寻找合适的解决方案并遇到这个问题的人。
I believe, what you truly want to achieve, is that a specific test ran before another test.我相信,你真正想要实现的是在另一个测试之前运行一个特定的测试。 The order itself is probably not that important.顺序本身可能不是那么重要。 In order to do that, you can add the @dependency annotation to a test.为此,您可以将@dependency注释添加到测试中。
Full documentation can be found here: https://codeception.com/docs/07-AdvancedUsage#Dependencies .完整文档可以在这里找到: https : //codeception.com/docs/07-AdvancedUsage#Dependencies Example:例子:

class ModeratorCest {

    public function login(AcceptanceTester $I)
    {
        // logs moderator in
    }

    /**
     * @depends login
     */
    public function banUser(AcceptanceTester $I)
    {
        // bans user
    }
}

要选择特定套件,或指定运行不同测试套件的顺序,您可以使用以下语法(无空格):

codecept run unit,functional,acceptance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM