How can I run only one test from a suite?

PhpCodeception

Php Problem Overview


I have this test class below, and I want to run only one test from it, for example the "aboutPage". Any ideas how?

This is how I run only this file:

codecept run tests/acceptance/VisitorCest.php

But now I want to run only one test from the file.

<?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');
    }
}

Php Solutions


Solution 1 - Php

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.)

Solution 2 - Php

this is what works:

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

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

Solution 3 - 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$

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

this is what I do.

php codecept.phar run unit UnitNameTest.php

Solution 7 - Php

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

Solution 8 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTzook Bar NoyView Question on Stackoverflow
Solution 1 - PhpTzook Bar NoyView Answer on Stackoverflow
Solution 2 - PhpMahmoud ZaltView Answer on Stackoverflow
Solution 3 - PhpconceptdeluxeView Answer on Stackoverflow
Solution 4 - PhpAkongnwi DevertView Answer on Stackoverflow
Solution 5 - PhpIgor ParraView Answer on Stackoverflow
Solution 6 - PhpYann叶View Answer on Stackoverflow
Solution 7 - PhpMuhammad ShahzadView Answer on Stackoverflow
Solution 8 - Phpatul_systematixView Answer on Stackoverflow