# Running single tests in Codeception

While undergoing a major architectural change in my framework I’ve found myself overwhelmed by the wall of failures and errors that Codeception threw once I launched an innocent

codecept run unit

from the project root. It’s easier to launch single tests when everything works.

## One test at a time

It’s possible to run one test at a time via codeception CLI interface via a command like

codecept run tests/unit/NamespaceResolverTest.php

but my problem is that, in my furious coding restructuring, I’ve broken many dependencies and the default bootstrap file, the _bootstrap.php in the tests/unit folder, will try to include many classes with missing or broken links.

## Create an alternative bootstrap file for PHPUnit

First approach to get around the block was to create an alternative PHPUnit bootstrap file loading only working classes and specifying its path while running PHPUnit

phpunit --colors --bootstrap tests/unit/_phpunit_bootstrap.php tests/unit/NamespaceResolver.php

It now works but it works because I’m running a PHPUnit test generated via Codeception with the command

codecept generate:phpunit unit NamespaceResolver

but many other tests I’d like to run are not PHPUnit tests at all.

## A better dynamic way

Codeception is very smart and a better way to do it is to configure either the local to the unit tests tests/unit/_bootstrap.php or the global tests/_bootstrap.php files to autoload classes that are requested.
Autoloading will include only classes that are called in code and since I’m running single tests file with

codecept run test/unit/NamespaceResolverTest.php

only the NamespaceResolver class will be included. I can either add in one of the above mentioned bootstrap files a straight require_once line (not so smart) like

// file  tests/unit/_bootstrap.php or tests/_bootstrap.php

require_once dirname(__FILE__) . '/path/to/class/to/include.php'

or use the very polite and convenient autoload structure Codeception provides (smart)

// file  tests/unit/_bootstrap.php or tests/_bootstrap.php

Codeception\Util\Autoload::register( 'TAD\\MVC\\Helpers', 'Resolver', dirname( dirname( __FILE__ ) ) . '/libs/mvc/helpers' );

that means

Codeception\Util\Autoload::register( 'TAD\\MVC\\Helpers',       // anytime I request a class in this namespace
'Resolver',                                                     // whose name ends in 'Resolver'
dirname( dirname( __FILE__ ) ) . '/libs/mvc/helpers' );         // go and look for it here

I can now run single tests via Codeception.