我如何才能在一个套件中只运行一个测试?

我在下面有这个测试类,我只想从它运行一个测试,例如“ about Page”。有什么办法吗?

这就是我只运行这个文件的方式:

codecept run tests/acceptance/VisitorCest.php

但是现在我只想从文件中运行一个测试。

<?php
use \AcceptanceTester;


class VisitorCest
{
public function _before(){}
public function _after(){}


public function aboutPage(AcceptanceTester $I)
{
$I->wantTo('check about page');
}


public function contactPage(AcceptanceTester $I)
{
$I->wantTo('check contact page');
}
}
48728 次浏览

You simply append a colon and the function name, like this:

codecept run tests/acceptance/VisitorCest.php:myTestName

or a shorter version:

codecept run acceptance VisitorCest:myTestName

(Notice the space between the suite-name and the file-name.)

this is what works:

codecept run {suite-name} {file-name}.php:{function-name}

notice the space between the suite-name and the file-name

this is what I do. php codecept.phar run unit UnitNameTest.php

In addition to the answer provided by @Tzook Bar Noy you can add a trailing $ when there are multiple tests which start with the same name. Consider the following example:

<?php


use \AcceptanceTester;


class VisitorCest
{
public function aboutPage(AcceptanceTester $I)
{
}


public function aboutPageOption(AcceptanceTester $I)
{
}
}

Where the following command will execute both of the tests:

codecept run tests/acceptance/VisitorCest.php:aboutPage

This will execute only the first:

codecept run tests/acceptance/VisitorCest.php:aboutPage$

In addition to the previous answers, you can run one or several methods by grouping by a given name:

/**
* @group test-aboutPage
*/
public function aboutPage(AcceptanceTester $I)
{
$I->wantTo('check about page');
}

Use the option -g and the name of the group:

$ codecept run acceptance VisitorCest -g test-aboutPage

If you are using PHP Yii2 Framework, then you can run only one test using this command.

Make sure you are in tests directory.

cd /codeception/frontend


codecept run -vv acceptance HomeCept

Try it phpunit --filter {TestMethodName} {FilePath}

A more proper way of doing this will be to assign a group annotation to the test case in question. This is preferable for the following reason; If you have two test cases for example in the same class VisitorCest;

public function aboutPage
public function aboutPage2

Executing

codecept run tests/acceptance/VisitorCest.php:aboutPage

will run both VisitorCest:aboutPage and VisitorCest:aboutPage2 test cases.

Assign a group to a test case like this

/**
* @group aaa
*/
public function aboutPage(AcceptanceTester $I)
{
}

And execute this particular test case like this

codecept run -g aaa

Today I learned more general answer to this question. There is a hidden way to run arbitrary list of Codeception tests. Let's say you want to run Cest1.php, Cest2.php, Cest3.php and nothing else. They are all from different suites, different groups, etc.

This is the sample code to achieve this:

codecept run -o "groups: custom: [Cest1.php, Cest2.php, Cest3.php]" -g custom

The trick is you define a group on-the-fly, include needed tests into it and execute tests from newborn group immediately in single command.