2

I followed Codeception's quick start guide (http://codeception.com/quickstart) and read their documentation (http://codeception.com/docs/05-UnitTests).

I have managed to set up the testing environment, and

  1. Created the unit test file (php codecept.phar generate:test unit ExampleTest)
  2. Run the test command (php codecept.phar run unit ExampleTest), which returns an error:

There was 1 error:

1) ExampleTest: Validation
 Test  tests\unit\ExampleTest.php:testValidation
                                  
  [Error] Class 'User' not found  
                                  
#1  ExampleTest->testValidation
#2  C:\laragon\www\kario\vendor\bin\codecept.phar:5

How does the test file know which PHP file to run the test on?

My laragon project is named kario, and sits in C:\laragon\www\kario\resources\views\pages\orders while the test unit file is in C:\laragon\www\kario\vendor\bin\tests\unit.

bschlueter
  • 3,817
  • 1
  • 30
  • 48
  • Have you got autoloading working in your application code? autoloading is your best option, but if you don't use it, then you will have to require_once each file separately. – Naktibalda Nov 07 '17 at 09:39

1 Answers1

0

I had this question too and found the answer for myself. Posted here http://phptest.club/t/beginner-codeception-unit-test-help/1849 but also, here you go:

First, some details. I am using Codeception v2.4.1, powered by PHPUnit 7.1.4. The answer is:

In codeception.yml, add these two lines:

    settings:
        bootstrap: _bootstrap.php

Here _bootstrap.php can be whatever you want the name of your bootstrap file to be.

You must place _bootstrap.php in each of the following directories as follows:

tests/unit/_bootstrap.php
tests/functional/_bootstrap.pp
tests/acceptance/_bootstrap.php

In my tests/unit/_bootstrap.php file, I placed the following code:

<?php
use Codeception\Util\Autoload;
Autoload::addNamespace('myclassnamespace', __DIR__ . '/../../Classes/');

To make sure I had the right path to Classes, I used trigger_error(__DIR__) in my _bootstrap.php before I added the Autoload line.

Then in my tests/unit/TestAddCest.php, I placed the following line at the beginning of the file:

<?php
use mynamespace;

And in my test function looks like this (note the instantiation of the User class):

    public function tryToTest(UnitTester $I)
    {
      $user = new mynamespace\User('someusername');
      $I->assertEquals('someusername', $user->username);
    }

I hand typed that function because I'm not on the same machine with the code and didn't feel like getting it over, so it may have a typo or bug, but you get the idea.

Edit 05/01/2018: someone else answered me on http://phptest.club:

It would be better to configure autoloading of your classes in composer.json and let Composer to do the rest, unless you try to avoid using Composer.

https://getcomposer.org/doc/01-basic-usage.md#autoloading

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
RJ Cole
  • 2,592
  • 3
  • 18
  • 23
  • I think your path for Autoload::addNamespace() would be __DIR__ . ‘\..\..\resources\views\pages\orders’ – RJ Cole Apr 28 '18 at 16:51