94

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');
    }
}
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82

9 Answers9

159

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

Simon East
  • 55,742
  • 17
  • 139
  • 133
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
  • just a note. you cannot run a single test if it is a **unit** test (extended from **PHPUnit_Framework_TestCase**) as **codeception** has no **filter** option (unlike **phpunit**) – coviex Jul 10 '15 at 11:59
  • 3
    codecept run unit/TestThatExtendsPHPUnit.php:testMethod works just fine for me. – mike.pj Dec 10 '15 at 21:15
  • 4
    You can omit the `.php` filename extension, like this: `codecept run -- -c frontend unit models/ContactFormTest:testSendEmail` – jlapoutre Nov 15 '16 at 10:48
  • 1
    `myTestName` doesn't have to be the full test name either. It will run any tests with a partial match – andrewtweber Jun 06 '19 at 21:31
  • 1
    How can I run with dependencies too? – Diogo Alves Jun 04 '20 at 16:58
41

this is what works:

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

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

Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
23

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$
conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29
14

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

Akongnwi Devert
  • 1,177
  • 12
  • 10
5

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
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • I would prefer slightly different way , since you can tab in that case and easily navigate to a desired Cest, ` codecept run tests/acceptance/VisitorCest -group test-aboutPage` – Stipe Apr 17 '17 at 21:12
2

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

Yann叶
  • 185
  • 1
  • 3
2

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
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
  • That's not related to Yii2, but to the fact that a `Cept` only contains one test, but a `Cest` can contain multiple tests - see the initial question – Oliver Hader Dec 06 '16 at 12:30
0

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.

Vasily
  • 981
  • 10
  • 12
-3

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

atul_systematix
  • 253
  • 3
  • 12