Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring File Names for PHPUnit

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.

like image 247
Steven Scott Avatar asked Jan 18 '12 19:01

Steven Scott


People also ask

What is the PHPUnit XML configuration file used to organize tests?

The <testsuite> Element.

What is PHPUnit XML file?

PHPUnit uses XML file for configuration. This configuration file can be added to your version control system so all developers get the same output. These settings will help you make sure your tests work the way you want.

What are PHPUnit fixtures?

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.


1 Answers

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.

like image 131
edorian Avatar answered Oct 05 '22 07:10

edorian