Tzook Bar Noy
Tzook Bar Noy

Reputation: 11677

How can I run only one test from a suite?

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

Upvotes: 98

Views: 56191

Answers (9)

Vasily
Vasily

Reputation: 1031

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.

Upvotes: 0

Akongnwi Devert
Akongnwi Devert

Reputation: 1347

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

Upvotes: 14

atul_systematix
atul_systematix

Reputation: 253

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

Upvotes: -3

Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

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

Upvotes: 2

Igor Parra
Igor Parra

Reputation: 10348

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

Upvotes: 5

Tzook Bar Noy
Tzook Bar Noy

Reputation: 11677

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

Upvotes: 165

conceptdeluxe
conceptdeluxe

Reputation: 3893

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$

Upvotes: 24

Yann叶
Yann叶

Reputation: 195

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

Upvotes: 2

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31190

this is what works:

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

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

Upvotes: 41

Related Questions