13

I'm using Codeception for unit, functional, and acceptance tests of my Laravel 4 PHP application.

My unit tests look this:

use Codeception\Util\Stub;
class ExampleTest extends \Codeception\TestCase\Test 
{
 public function testExample()
 {
  $example = true;
  $this->assertSame($example, true);
 }
}

My functional tests look like this:

use \TestGuy;
class ExampleCest
{
 public function example(TestGuy $I)
 { 
  $I->amOnPage('/auth/login');
  $I->see('Sign in');
 }
}

But I also want to use PHPUnit assert methods in my functional tests. But when I try to, I get this error:

Call to undefined method ExampleCest::assertSame()

How do I use PHP assert methods in a Codeception functional test?

mtmacdonald
  • 14,216
  • 19
  • 63
  • 99

5 Answers5

32

Since Codeception 2.1 (not 2.0) you can use it like the other asserts with:

$I->assertSame($expected, $actual, $message);

But don't forget to enable the Asserts module in your config - e.g.:

class_name: UnitTester
modules:
    enabled: [ Asserts ]

Please note: You might need to change your configuration when upgrading to 2.1 - see upgrade instructions: http://codeception.com/06-19-2015/codeception-2.1-rc.html

conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29
  • 4
    This is the best answer here for the question of "how to use the Assert methods in Functional tests". – lintuxvi Dec 07 '16 at 19:13
  • 3
    In order to see additional methods in your IDE, you have to run test suite once after config change. From codeception docs: "By default, Codeception automatically rebuilds the Actions trait on each change of the suite configuration." – Konrad Gałęzowski Dec 14 '16 at 12:52
13

\PHPUnit_Framework_Assert::assertSame()

mdml
  • 22,442
  • 8
  • 58
  • 66
MrBinWin
  • 1,269
  • 1
  • 16
  • 30
5

In Codeception 4 just add the Asserts Module:

modules:
    enabled:
        - \Codeception\Module\Asserts

to your suite.yml config file and run codeception build

Tavo Nieves J
  • 101
  • 2
  • 2
2

Another workaround can be to use Helper Methods in test suite.

For example for assertSame() method

class ExpectedHelper extends \Codeception\Module
{
    protected $test;

    function _before(\Codeception\TestCase $test) {
        $this->test = $test;
    }

    function assertSame($expected, $actual, $message = '')
    {
        $this->test->assertSame($exception, $actual, $message);
    }
}

where ExpectedHelper being the test suite Helper name (eg: UnitHelper, FunctionalHelper) which should be under _support folder

and you can use it in your test as $I->assertSame('12340','12340');

Naresh
  • 31
  • 2
0

I was in the same problem and somehow figured it out. Here I am going to share what I did to achieve my goal. Step 1: Install Codeception assert module OR simply add the following module to composer.json file and then update composer.

"codeception/module-asserts": "^3.0",

Step 2: Then it requires to enable the assert module by adding the following module in the *.suite.yml file.

modules:
    enabled:
        - \Codeception\Module\Asserts:

And finally build Codeception once again by codecept build OR codeception build whichever alias works for you.