Phpunit: Test suite naming conventions

Created on 12 Feb 2016  ยท  2Comments  ยท  Source: sebastianbergmann/phpunit

The manual highlights some conventions:

  • The tests for a class MyClass go into a class MyClassTest
  • The class MyClassTest live in file MyClassTest.php
  • MyClassTest inherits from PHPUnit_Framework_TestCase
  • Tests are public methods that are named test*

This will result in something like this folder structure:

โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ classes/
โ”‚   โ”‚   โ”œโ”€โ”€ MyClass.php # Different
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ testcases/
โ”‚   โ”‚   โ”œโ”€โ”€ MyClassTest.php # Different
โ”‚   โ”œโ”€โ”€ bootstrap.php
โ”‚   โ””โ”€โ”€ ...
โ””โ”€โ”€ ...

... and this test case:

MyClassTest extends PHPUnit_Framework_TestCase {
    testMyMethod() {
        // Code here.
    }
}

My question
I'm wondering why the naming used inside the test suite can't mirror the project's source code? For example, file names can match:

โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ classes/
โ”‚   โ”‚   โ”œโ”€โ”€ MyClass.php # Same
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ testcases/
โ”‚   โ”‚   โ”œโ”€โ”€ MyClass.php # Same
โ”‚   โ”œโ”€โ”€ bootstrap.php
โ”‚   โ””โ”€โ”€ ...
โ””โ”€โ”€ ...

And if using PHP > 5.3, namespaces can be used to allow class names to match:

namespace MyProject\MyTests;
MyClass extends PHPUnit_Framework_TestCase { # The class name MyClass matches the class name used in my project's source. 
    /**
     * @test
     */
    MyMethod() {  The method name MyMethod matches the method name used in my project's source. 
        // Code here.
    }
}

Note the @tests annotation is used so method names can match.

All 2 comments

This is not a discussion / support forum, sorry.

:cry:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dciancu picture dciancu  ยท  3Comments

kunjalpopat picture kunjalpopat  ยท  4Comments

stephen-leavitt-sonyatv-com picture stephen-leavitt-sonyatv-com  ยท  4Comments

nicklevett picture nicklevett  ยท  4Comments

greg0ire picture greg0ire  ยท  4Comments