How to exclude file from PHPUnit test suite in xml config?

前端 未结 7 610
夕颜
夕颜 2021-02-02 06:39

I have following, very simple, XML config for PHPUnit:


    
        

        
相关标签:
7条回答
  • 2021-02-02 07:15

    Whit this PHPUnit configuration-file I have made very good experiences.

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        colors="true"
        processIsolation="true"
        stopOnFailure="true"
        syntaxCheck="false"
        backupGlobals="false"
        bootstrap="test-bootstrap.php">
        <testsuites>
            <testsuite name="php-dba-cache">
              <directory suffix="Test.php">tests</directory>
            </testsuite>
        </testsuites>
        <logging>
            <log type="coverage-html"
                 target="build/coverage"
                 charset="UTF-8"
                 yui="true"
                 highlight="true"
                 lowUpperBound="35"
                 highLowerBound="70"/>
       </logging>
        <filter>
            <whitelist addUncoveredFilesFromWhitelist="true">
                <directory suffix=".php">src</directory>
                <exclude>
                 <file>test-bootstrap.php</file>
                </exclude>
            </whitelist>
        </filter>
    </phpunit>
    

    https://github.com/gjerokrsteski/php-dba-cache

    0 讨论(0)
  • 2021-02-02 07:20

    To exclude the file name TestCase.php.

    add this to your phpunit.xml

    <testsuites>
        <testsuite name="BLABLA">
            <directory suffix=".php">./tests</directory>
            <exclude>./tests/TestCase.php</exclude>
        </testsuite>
    </testsuites>
    

    Here is an additional excerpt from a real-live test-suite I can confirm it working with:

    ...
        <testsuites>
            <testsuite name="n98-magerun-tests">
                <directory>./tests</directory>
                <exclude>tests/N98/Magento/Command/Installer/UninstallCommandTest.php</exclude>
            </testsuite>
        ...
    
    0 讨论(0)
  • 2021-02-02 07:27

    The phpunit documentation is a bit minimalistic when it comes to exclusion in a testsuite. Apparently, only entire directories can be excluded but not individual files. I would be very happy to be proven wrong. The workaround seems to be using the @group feature as posted above by Alister Bulman.

    It's kind of a pain needing to tag every single test in those test suites I'd like to keep.

    0 讨论(0)
  • 2021-02-02 07:27

    Hey there, Make sure that you put your exclusions in the Whitelist. Example:

    <phpunit>
        <filter>
            <blacklist>
                <directory suffix=".php">/not/even/looked/at/</directory>
            </blacklist>
            <whitelist>
                <directory suffix=".php">/path/to/test/dir/</directory>
                <exclude>
                    <file suffix=".php">/path/to/fileToExclude.php</file>
                </exclude>
            </whitelist>
        </filter>
    </phpunit>
    

    http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist

    0 讨论(0)
  • 2021-02-02 07:28

    Along with the solutions above you could use more architecture-driven testing workflow where you manage your tests, directories and testsuites from phpunit.xml like this:

    • Group your tests in the directories (e.g. tests/Unit, tests/Feature);
    • Make testsuites as you see needed grouping your directories with <testsuite> element;
    • Make an All testsuite that combines all default tests you would run as a full test suite and assign it via defaultTestSuite="All" key within <phpunit> element.
    • Make a dedicated Tinker test suite with tests that you could use for tinkering, keeping example tests etc. you would exclude from normal testing workflow. Do not inlcude it in the All test suite.

    So you will be able to:

    • use phpunit CLI command to always run the default All tests.
    • use CLI to filter on testsuite, test file or single test level for any of your test suites e.g.:
      • phpunit --testsuite SuiteOne,
      • phpunit --filter SomeTest or
      • phpunit --filter SomeTest::test_some_test_method
      • combine --testsuite with --filter arguments

    Couple this workflow with the capability to run the current test or test file from within your IDE (for Sublime Text there are Mac and Linux/Windows plugins) and you will be completelly equipped to instantly chose what test to execute.

    0 讨论(0)
  • 2021-02-02 07:30

    There are a number of ways to not run a particular test - putting it into a blacklist so it's never run may not be the way - as changing it means editing the blacklist, and you'll often endup bouncing it in and out of version control.

    There are several other ways that may be more appropriate:

    If a test is not yet ready to run:

    $this->markTestIncomplete('This test has not been implemented yet.');
    

    If there's an outside reason it should not be run, skip it:

    if (!extension_loaded('mysqli')) {
        $this->markTestSkipped('The MySQLi extension is not available.');
    }
    

    You can also put that into the setUp() function, so it will skip all the tests in a test-class.

    You can make a test dependant on a previous one succeeding:

    public function testEmpty()
    {
        $stack = array();
        $this->assertTrue(empty($stack));
        return $stack;   // also sends this variable to any following tests - if this worked
    }
    /**
     * only runs if testEmpty() passed
     *
     * @depends testEmpty
     */
    public function testPush(array $stack)
    {
    }
    

    The @group -name- annotation is one of the best ways to specifically stop, or run one group of tests

    /**
     * @group database
     * @group remoteTasks
     */
    public function testSomething()
    {
    }
    

    testSomething() is now in two groups, and if either is added on the command line (or in the config.xml) --exclude-group parameter. it won't be run. Likewise, you could run only tests that belong to a particular group - say named after a feature, or bug report.

    0 讨论(0)
提交回复
热议问题