Unit Testing II - Command Line Testing

Running unit tests from the command line

In the previous article in this series, we looked at setting up the testing environment for running unit tests. In this one, we'll see how to run tests from the command line.

MODX logo

Running Tests from the Command Line

You can run individual test files in a suite, but you can also run all the tests in a suite in sequence with a single command. In fact, you can run all tests in all suites with a single command. In all cases a report will be generated. It will appear in your terminal, or you can redirect it to a file.

From the _build directory (assuming that all the tests have been set up properly) you can:

Run a single test file in one suite:

codecept run unit test1

Run a single test method inside a test file:

codecept run suite_name file_name:method_name

For example:

codecept run unit UserTest:TestConstructorWithparams

The command must be run from the directory above the tests directory (_build in our example). Notice that you don't need to specify the tests directory, just the filename and method name. In fact, if you do specify it, the test won't run.

Run all unit tests:

codecept run unit

Run tests in multiple suites:

codecept run unit,functional,acceptance

To write the report to a file, just append this to the run command:


The main configuration file for all your tests is codeception.yml. You should see it in your _build directory. Codeception needs to find this file to run any tests. You can run the test from anywhere if you specify the location with the -c flag. For example, from the tests directory (just below _build:

codecept run -c ../codeception.yml

I find that life is much easier, though, if I just go to the _build directory to run tests. You can create an alias or batch file to take you there. You could also create an alias that would change codeception run into codeception run -c path/to/codeception.yml

A full codeception.yml file for our tests is available at GitHub, here.

You can cut and paste the content from the file, but if you are using an older version of Codeception, you may need to correct the _bootstrap code depending on your version of Codeception as described in the previous article. The version at GitHub is for later versions of Codeception.

In the tests directory, you'll should find the files unit.suite.yml, integration.suite.yml, functional.suite.yml, and acceptance.suite.yml. These are the configuration files for each suite. You'll seldom need to modify codeception.yml once you have it the way you want it, but you often have to edit the suite configuration files.

You'll also see some directories that start with an underscore: _data, _output, and _support with some further directories below them. These are places to put stored data to use in tests, test output or logs, helper functions, etc. There are also some generated files used internally by Codeception. We won't be using these for a while. When running tests, Codeception ignores both directories and files that begin with an underscore, but you can use them directly in any test if you need them.

Coming Up

In the next article, we'll create and run our first unit test.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.