스위트에서 하나의 테스트 만 실행하려면 어떻게해야합니까?


87

아래에이 테스트 클래스가 있으며 여기에서 "aboutPage"와 같은 하나의 테스트 만 실행하고 싶습니다. 어떻게 아이디어가 있습니까?

이 파일 만 실행하는 방법입니다.

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

답변:


142

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


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

3
codecept run unit/TestThatExtendsPHPUnit.php:testMethod works just fine for me.
mike.pj

4
You can omit the .php filename extension, like this: codecept run -- -c frontend unit models/ContactFormTest:testSendEmail
jlapoutre

1
myTestName doesn't have to be the full test name either. It will run any tests with a partial match
andrewtweber

How can I run with dependencies too?
Diogo Alves

40

this is what works:

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

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


technically you have the more general solution. but as proven, most people like seeing examples instead.
iGbanam

2
Do not need the .php part.
tivnet

2
@tivnet This is only true for Codeception 2.x
conceptdeluxe

22

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$

10

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


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

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

2

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


Irrelevant to the question asked
tivnet

1

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

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

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.