[英]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:我希望能够在两个级别上控制测试的顺序:
unit tests
before acceptance tests
)acceptance tests
之前运行unit tests
acceptance tests
)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
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.