8

I am a new user of PHPUnit, and I am converting our existing tests (asserts) into the PHPUnit framework to provide a better test environment and code coverage. However, I need to know how to try to get PHPUnit working with our testing code structure.

Our project directories are similar to the following:
Application1/
   CREDIT_CARD.class - Class naming convention for CREDIT_CARD
   CREDIT_CARD.class.test - Automated Tests for CREDIT_CARD.class
   File.php - Application File
   File.php.test - Automated tests for File.php
   File2.php
   File2.php.test - Automated tests for File2.php

Application2/
   ANOTHER_CLASS.class
   ANOTHER_CLASS.class.test
   DifferentFile.php - Application File
   DifferentFile.php.test - Automated tests for File.php

lib/
   UTIL/
      SHARED_CLASS.class
      SHARED_CLASS.class.test
   VISUAL/
      VISUAL_TOOL.class
      VISUAL_TOOL.class.test

I need to know how to configure the PHPUnit tests so I can run the tests in lib/UTIL/.test (which load the class file using the setUp() method) then the lib/VC/.test, followed (if successful) by the Application1 and Application2 tests. I saw mention of a PHPUnit_xml file and a bootstrap file, but I can not find a reference template to see if these are what I need. Any help would be appreciated.

I know the documentation refers to a test.php addition to the file names, but I am hoping to not have to change our structure and naming conventions as I would like to be able to run a mix of the files until they are all converted over to the PHPUnit framework. Changing names will cause a procedure change in our company and training for the developers, which I am trying to avoid.

Thanks in advance for any assistance.

edorian
  • 38,542
  • 15
  • 125
  • 143
Steven Scott
  • 10,234
  • 9
  • 69
  • 117
  • So `Filename.class.test` does contain a class that extends PHPUnit_Framework_TestCase? Did i get that right? – edorian Jan 18 '12 at 20:43
  • Yes. class TEST_SOME_CLASS extends PHPUnit_Framework_TestCase { protected function setUp() { if ( ! class_exists('SOME_CLASS')) require_once(substr(__FILE__, 0, -5)); // strip '.test' extension } public function testConstants() { $this->assertEquals(SOME_CLASS::TYPE_1, 1); $this->assertEquals(SOME_CLASS::TYPE_2, 2); } } – Steven Scott Jan 20 '12 at 20:15

1 Answers1

8

So you have files named Foo.class.test instead of the PHPUnit default FooTest.php ?

That shouldn't be a big problem as PHPUnit allows you to configure what file suffixes are to be treated as "tests". The Test.php is just the default.

Have a look at the xml config part of the docs regard test organisation

What you want is:

 <testsuites>
    <testsuite name="Our new shiny phpunit test Suite">
      <directory suffix=".test">./sourceFolder</directory>
    </testsuite>
  </testsuites>

PHPUnit will then scan through the source folder including all files that end in .class.test and leaving the other files as is.

edorian
  • 38,542
  • 15
  • 125
  • 143
  • As there are some non-class test files, this might need to be `suffix=".test"`. – David Harkness Jan 18 '12 at 22:46
  • Thanks for the suggestions. Any chance of possibly getting a sample file posted with some of these suggestions, since I do have different files, and not much experience with PHPUnit? I am looking at the reference, and am mostly just looking for a base file to start from to trial and error and check understandings with the document. – Steven Scott Jan 20 '12 at 20:11
  • It should also be noted that there are some files with a .fn extension, and JavaScript and other files. IE: FunctionCode.fn, JavaScript.js, Application.css, etc... – Steven Scott Jan 20 '12 at 20:13
  • Thanks. That solved my problem. Sorry for the delay in getting back to update this, but work priorities are always moving, hence the big need for automated testing. – Steven Scott Mar 22 '12 at 13:53
  • 1
    link updated: https://phpunit.de/manual/4.3/en/appendixes.configuration.html#appendixes.configuration.testsuites – velcrow Oct 16 '14 at 22:03
  • 1
    Is this the new preferred hyperlink? https://phpunit.readthedocs.io/en/9.5/configuration.html#appendixes-configuration-testsuites – mickmackusa Feb 08 '21 at 01:37